我有一個標籤,我想使用addGestureRecognizer
。我把它放在cellForRowAtIndexPath
,但是當我做print(label.text)
時,它從另一個單元打印標籤。但是當我將它放入didSelectRowAtIndexPath
時,它會打印出該單元的正確標籤。點擊表格視圖中的標籤?
解決此問題的最佳方法是什麼?
下面是代碼:
var variableToPass: String!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : MainCell! = tableView.dequeueReusableCellWithIdentifier("MainCell") as! MainCell
variableToPass = label1.text
cell.label1.userInteractionEnabled = true
let tapLabel = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapLabel(_:)))
cell.label1.addGestureRecognizer(tapLabel)
return cell as MainCell
}
func tapCommentPost(sender:UITapGestureRecognizer) {
print(variableToPass)
}
您可以顯示的tableview執行'cellForRowAtIndexPath'代碼和動作代碼 –
使用自定義的UITableViewCell類。 –
@ Anbu.Karthik剛剛編輯過文章 – johnniexo88