Tap label in table view?
<p>I have a label I want to tap on using <code>addGestureRecognizer</code>. I put it in <code>cellForRowAtIndexPath</code> but when I do <code>print(label.text)</code>, it prints a label from another cell. But when I put it in <code>didSelectRowAtIndexPath</code>, it prints out the right label for that cell.</p>

<p>What is the best way to fix this?</p>

<p>Here is the code:</p>

<pre><code>var variableToPass: String!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; 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)
    }
</code></pre>