Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crashing when calling indexPathForCell [duplicate]

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!

like image 857
user2535943 Avatar asked Jun 30 '13 06:06

user2535943


1 Answers

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.

like image 76
Avi Tsadok Avatar answered Nov 14 '22 12:11

Avi Tsadok