Topaz t
Topaz t

Reputation: 165

How do you get the indexpath.row when a button in a cell is tapped?

When a button in a table row cell is pressed, it will activate this function ( just print the indexrow number of the row pressed)

@IBAction func tickAction(sender: UIButton) {

    let button = sender as UIButton
    let view = button.superview!
    let cells = view.superview as TableViewCell

    let indexPath = TableViewController.indexPath(cells)//itemTable.indexPathForCell(cells)   <--ERROR here

    println(indexPath)
}

Upvotes: 0

Views: 1635

Answers (2)

Shamas S
Shamas S

Reputation: 7549

There are a bunch of ways you can do this.

One of the most easiest is to get the location of the button tapped and look up indexPath for that point. Something like

CGPoint buttonPosition = [buttonPressed convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

Upvotes: 1

Sumit Oberoi
Sumit Oberoi

Reputation: 3475

1) create a custom table view cell

2) In that class create an IBAction for button

3) Create a delegate of custom cell

4) conform to protocol in your view controller

5) set delegate in cellforRowIndexpath cell

6) now you can access button tap in delegate method

Upvotes: 1

Related Questions