I have a UITableView with custom cells. Within each UITableViewCell, there is a UIButton. I'm attempting to find out which cell the button is located in when it is tapped. To do this I've done this:
- (IBAction)likeTap:(id)sender {
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
UITableView* table = (UITableView *)[buttonCell superview];
NSIndexPath *pathOfTheCell = [table indexPathForCell:buttonCell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);
I thought this would work fine, but when indexPathForCell is called, an exception is thrown.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell indexPathForCell:]: unrecognized selector sent to instance 0x756d650'
Any ideas about what I've done wrong? Thanks!
This is your problem:
(UITableViewCell *)[senderButton superview]
It should be:
(UITableViewCell *)[[senderButton superview] superview]
Because the superview of the button is not the cell, is the contentView which subview of the cell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With