2016-04-17 12 views
1

私はユーザーが1つのセルで2つの画像のいずれかを選択するアプリケーションを開発しています。どのテーブルセルがスウィフトでクリックされたかを検出する方法2

prototype cell

がどのように私は、ユーザーが選択されているセルの投票ボタンを押したときに検出することができます:私のプロトタイプセルは次のようになりますか?

私のtableViewコード:

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

     let cellIdentifier = "NewTableViewCell" 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NewTableViewCell 

     //For left image 
     if let url:NSURL? = NSURL(string: self.polls[indexPath.row].poll_photo1){ 
      cell.leftImage.sd_setImageWithURL(url) 
     } 
     //For right image 
     if let url:NSURL? = NSURL(string: self.polls[indexPath.row].poll_photo2){ 
      cell.rightImage.sd_setImageWithURL(url) 

     } 

     //For user picture 
     if let url:NSURL? = NSURL(string: self.polls[indexPath.row].users[0].user_photo){ 
      cell.userPicture.sd_setImageWithURL(url) 
     } 

     // gets username and text 
     cell.userName.text=self.polls[indexPath.row].users[0].user_name 
     cell.description.text = self.polls[indexPath.row].poll_textfield 

     return cell 
    } 

マイウェブAPI:

enter image description here

+0

最初に投票をどのように処理していますか? –

+0

'NewTableViewCell'クラスで扱われるようです。 – trojanfoe

+1

私はあなたのスウィフトコードを意味しました。あなたはまだ何か試してみませんか? –

答えて

4

私はあなたのカスタムテーブルビューセルNewTableViewCellがあなたの投票ボタンの出口を有すると仮定しています。
voteButtonindexPath.rowとタグを付けるだけで、ターゲット機能でタグを次のように取得できます。あなたは、あなたの投票ボタン

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

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

    //Tagging with indexPath.row 
    cell.voteLeftButton.tag = indexPath.row 
    cell.voteRightButton.tag = indexPath.row 


    //This is the latest Swift 2.2 syntax for selector. If you are using the older version of Swift, Kindly check the selector syntax and make changes accordingly 
    cell.voteLeftButton.addTarget(self, action: #selector(voteLeftButtonPressed), forControlEvents: .TouchUpInside) 
    cell.voteRightButton.addTarget(self, action: #selector(voteRightButtonPressed), forControlEvents: .TouchUpInside) 
    return cell 

} 

func voteLeftButtonPressed(sender:UIButton){ 

    print("Left Button Table cell clicked is \(sender.tag)") 

} 

func voteRightButtonPressed(sender:UIButton){ 

    print("Right Button Table cell clicked is \(sender.tag)") 

} 
+0

私はIBOutletを使うべきですか?私は2つのボタンIBAction leftVoteとIBAction rightVoteを持っていますか?代わりにIBAction – tmac99

+0

はい、あなたのカスタムセルクラス –

+0

ではなくIBActionsを作成して両方の 'IBOutlet'を作成し、私が投稿したソリューションのように' cellForRowAtIndexPath'でターゲットを作成してください –

0

は、このような細胞configureメソッドに、ボタンにターゲット/アクションを追加します押したときvoteButtonがタップされたセルの知るようになるだろう:

let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourController.tapAction(_:))) 

その後tapAction方法

を実装します
func tapAction(sender : UITapGestureRecognizer) 
    { 

    if sender.state != UIGestureRecognizerState.Ended 
    { 
     return 

    } 
    let btn = sender.view as! UIButton 

    let pointTo : CGPoint = CGRectOffset(btn.bounds, btn.frame.size.width/2, btn.frame.size.height/2).origin; 
    let buttonPosition : CGPoint = btn.convertPoint(pointTo, toView: self.tableView) 

    let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition) 


    ... 

} 
+0

'UIButton'の場合、なぜタップジェスチャーを' UIButton'に与えるのですか? 'UIButton'自体は' UIControl'から継承し、さまざまなイベントに対応しています –

+0

はい、そうです。 UIImageViewに使用したい場合は、私のやり方はもっと普遍的です – nerowolfe

関連する問題