2016-08-10 7 views
0

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) 
    } 
+2

あなたは 'cellForRowAtIndexPath'コードとジェスチャーコード –

+0

はテーブルビューを実装するためのカスタムUITableViewCellのクラスを使用し表示することができます。 –

+0

@ Anbu.Karthikは投稿を編集しました – johnniexo88

答えて

1

私はあなたが、たとえば、tap.tag = indexPath.rowを設定するための検索のためにあなたはタブ付きのどのセルを識別することを忘れ考える

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.tag = indexPath.row 
     tapLabel.numberOfTapsRequired = 1 
     cell.label1.addGestureRecognizer(tapLabel) 



     return cell as MainCell 
    } 

func tapLabel(sender:UITapGestureRecognizer) { 

    let searchlbl:UILabel = (sender.view as! UILabel) 
    variableToPass = searchlbl.text! 
    print(variableToPass) 
    } 
+0

上記の私の編集した質問に従って回答を変更してください。 johnniexo88 @ – johnniexo88

+0

- 屋johnniexo88 @ –

+0

てください - 私は取得更新答え –

0

あなたの現在のコードにはいくつかの問題があります。 :(1)variableToPasscellForRowAtIndexPath:に設定しているので、label1.textがセルに属するラベルであると仮定すると、テーブルがロードされると、variableToPassには最後にロードされたセルのラベルテキストが常に含まれます。 (2)cellForRowAtIndexPath:は、1つのセルに複数のジェスチャ認識機能を追加できるように、各セルに対して複数回呼び出すことができます(たとえば、スクロールするなど)。

問題1を解決するには、variableToPass変数を完全に削除して、ジェスチャのラベルビューに直接アクセスしてください。問題#2を解決するには、カスタムのMainCellテーブルビューセルにジェスチャ認識機能を追加することをおすすめしますが、そうしたくない場合は、ジェスチャ認識機能を追加してください。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("MainCell") as! MainCell 

    if cell.label1.gestureRecognizers?.count == 0 { 
     cell.label1.userInteractionEnabled = true 

     let tapLabel = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapCommentPost(_:))) // I assume "tapLabel" was a typo in your original post 
     cell.label1.addGestureRecognizer(tapLabel) 
    } 

    return cell 
} 

func tapCommentPost(sender:UITapGestureRecognizer) { 
    print((sender.view as! UILabel).text) // <-- Most important change! 
} 
関連する問題