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)
}
あなたは 'cellForRowAtIndexPath'コードとジェスチャーコード –
はテーブルビューを実装するためのカスタムUITableViewCellのクラスを使用し表示することができます。 –
@ Anbu.Karthikは投稿を編集しました – johnniexo88