Search code examples
uitableviewswiftuibuttoncustom-cell

Detect UIButton while using custom UITableViewCell


I want to implement a custom UITableViewCell class that is made up of subviews such as, labels, buttons, and images. These cells will display content fetched from the web using an API.

I do not want to implement the UITableView delegate method didSelectRowAtIndexPath as this will make the entire cell selectable. Only the button in the cell should be able to trigger any action.

The button is connected from the storyboard to the custom UITableViewCell via IBOutlet. The addTarget(_:action:forControlEvents:) method is called on the UIButton in cellForRowAtIndexPath of the UITableViewController class.

The roadblock occurs when we want to detect the indexPath of the selected cell in the Selector function of the button.

This is how the indexPath for the selected cell can be detected in the Selector function

@IBAction func doSomething(sender: AnyObject) {
    var location: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(location)!
    println("The indexPath for Selected Cell is - \(indexPath.row)")
}

Although, this successfully gets me around the issue, my question is;

1) Have you found an alternate way of being able to use UIButtons to pass selected cell data in a custom UITableViewCell?

2) What would be the best practice, so far, in Swift to implement a similar scenario?


Solution

  • If your tableView only has one section, you can use the tag property of the button to store the indexPath.row.

    In cellForRowAtIndexPath, when you set the target-action for the button, set button.tag = indexPath.row.

    Then in your doSomething routine:

    @IBAction doSomething(sender: UIButton) {
        println("This button is from row - \(sender.tag)")
    }