我有一個自定義單元格,它具有名爲StatusText的UITextView。我有一個擴展名,它可以在textView中找到所有@符號並將其變爲鏈接。我希望能夠點擊該用戶名鏈接並轉到配置文件視圖控制器。自定義單元格中的鏈接UITextView註冊分接頭
這裏是CustomCell的下面的代碼:
import UIKit
class profileCell: UITableViewCell, UITextViewDelegate {
@IBOutlet weak var UserImage: UIImageView!
@IBOutlet weak var UserName: UILabel!
@IBOutlet weak var Time: UILabel!
@IBOutlet weak var Likes: UILabel!
@IBOutlet weak var Dislikes: UILabel!
@IBOutlet weak var StatusText: UITextView!
@IBOutlet weak var LikeButton: UIButton!
@IBOutlet weak var DislikeButton: UIButton!
override func awakeFromNib() {
StatusText.delegate = self
}
func StatusText(StatusText: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
/* perform your own custom actions here */
print("firing this right now")
return true // return true if you still want UIAlertController to pop up
}
}
這是完美的,問題是,當鏈接被點擊時,函數不會觸發。我試着把這個函數放在customCell.swift類中,但是當鏈接被點擊時什麼也沒有發生。它被設置爲被選中。我認爲該功能無法找到單元格內的鏈接。 –
@MatthewFoster,你可以驗證'awakeFromNib'被調用,並且'StatusText'在這個時候被實例化嗎? –
所以awakeFromNib在tableView第一次加載視圖控制器時被調用。在customCell單元格可見時,向上和向下滾動表格,awakeFromNib不會觸發。 –