Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep track of the index path of a button in a table view cell?

Tags:

I have a table view where each cell has a button accessory view. The table is managed by a fetched results controller and is frequently reordered. I want to be able to press one of the buttons and obtain the index path of that button's table view cell. I've been trying to get this working for days by storing the row of the button in its tag, but when the table gets reordered, the row becomes incorrect and I keep failing at reordering the tags correctly. Any new ideas on how to keep track of the button's cell's index path?

like image 358
Jake Avatar asked May 19 '10 08:05

Jake


People also ask

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

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

Which method is used to allow moving of row in Uitableview?

moveRow(at:to:) Moves the row at a specified location to a destination location.


1 Answers

If you feel uncomfortable relying on button.superview, this method should be a little more robust than some of the other answers here:

UIButton *button = (UIButton *)sender; CGRect buttonFrame = [button convertRect:button.bounds toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin]; 
like image 157
wxactly Avatar answered Oct 05 '22 05:10

wxactly