点击表格视图中的标签?

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

转载请注明出处:http://www.cntdm.com/article/20230521/2470095.html