tmac99
tmac99

Reputation: 153

How to detect which table cell is clicked in swift 2

I am developing app which users will choose one of the two pictures in one cell. My prototype cell looks like :

prototype cell

How can I detect when the user presses the vote button which cell is selected ?

My tableView Code :

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
    }

My web API:

enter image description here

Upvotes: 1

Views: 985

Answers (2)

Rajan Maheshwari
Rajan Maheshwari

Reputation: 14571

I am assuming that your custom table view cell NewTableViewCell is having an outlet for your vote button.
Just tag your voteButton with indexPath.row and fetch the tag in its target function as shown below. You will get to know which cell's voteButton was tapped when you press your Vote Button

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)")

}

Upvotes: 4

nerowolfe
nerowolfe

Reputation: 4817

Add target/action to your button in cell configure method like this:

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

Then implement tapAction method

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)


    ...

}

Upvotes: 0

Related Questions