1

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?

3
  • By the way your sender is of type AnyObject so calling convertPoint:toView without casting it to a UIView would result in a compile error.
    – Jawwad
    Feb 4, 2015 at 12:50
  • I agree. I thought I would get a compile error too, but the @IBAction func mentioned above works without any such errors. Although, making 'sender' of the type UIView would be a comparatively cleaner approach.
    – Lester
    Feb 5, 2015 at 4:45
  • Interesting. Maybe it doesn't give a compile error since its entirely possible that the sender being an AnyObject could possibly implement the convertPoint:toView method.
    – Jawwad
    Feb 5, 2015 at 4:53

2 Answers 2

2

One way would be to iterate over the superviews of the sender and see if a UITableViewCell can be found...

Let's say you created a generic UIView extension with a method that would check to see if any of its superview's was a UITableViewCell...

extension UIView {
    func parentTableViewCell() -> UITableViewCell? {
        var view = self
        while let superview = view.superview {
            if let cell = superview as? UITableViewCell {
                return cell
            } else {
                view = superview
            }
        }
        return nil
    }
}

Then you could do the following...

if let cell = (sender as UIView).parentTableViewCell() {
    let indexPath = tableView.indexPathForCell(cell)
    println("The row for this cell is - \(indexPath.row)")
}

Another way would be to use the tag property of a view by setting it to an Int and then check to see what the tag of the sender was in the method.

i.e. In your tableView:cellForRowAtIndexPath: method

cell.myButton.tag = indexPath.row

Then in your doSomething method

let rowIndex = (sender as UIButton).tag
println("The row for this cell is - \(rowIndex)"
1

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)")
}
1
  • This approach works as well. Much more concise than my approach.
    – Lester
    Feb 5, 2015 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.