我有一个标签,我想使用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