2013-06-30 30 views
5

Mam UITableView z komórkami niestandardowymi. W każdej komórce UITableViewCell znajduje się UIButton. Próbuję dowiedzieć się, w której komórce znajduje się przycisk, kiedy zostanie dotknięty. Aby to zrobić, zrobiłem to:Zawieszenie podczas wywoływania indexPathForCell

- (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); 

myślałem, że to będzie działać dobrze, ale kiedy indexPathForCell nazywa, jest wyjątek.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell indexPathForCell:]: unrecognized selector sent to instance 0x756d650' 

Jakieś pomysły na temat tego, co zrobiłem źle? Dzięki!

Odpowiedz

6

To jest twój problem:

(UITableViewCell *)[senderButton superview] 

Powinno być:

(UITableViewCell *)[[senderButton superview] superview] 

Ponieważ podgląd przycisku nie jest komórką, jest to contentView, który podzbiór komórki.

+0

Zadziałało! Dzięki! – user2535943

+0

Uwaga: będzie działać na iOS7, ale nie na iOS6! –

+0

oszczędzasz mój czas, wiele razy. – Vigor

2

Dlaczego dont ustawić znacznik dla każdego przycisku w swojej metodzie cellForRowAtIndexPath:

button.tag = indexPath.row; 

a następnie w metodzie masz:

- (IBAction)likeTap:(id)sender { 
    NSLog(@"rowofthecell %d", button.tag); 
} 
1

Albo można ustawić znacznik dla każdego przycisku jak sugeruje edzio27 lub można spróbować użyć introspection jak pokazano poniżej:

- (IBAction)likeTap:(UIButton *)sender { 

    UIButton *senderButton = (UIButton *)sender; 

    if ([senderButton.superView isKindOfClass:[UITableViewCell class]]) { 
     UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview]; 

     if ([buttonCell.superView isKindOfClass:[UITablewView class]]) { 
      UITableView* table = (UITableView *)[buttonCell superview]; 

      NSIndexPath *pathOfTheCell = [table indexPathForCell:buttonCell]; 
      NSInteger rowOfTheCell = [pathOfTheCell row]; 
      NSLog(@"rowofthecell %d", rowOfTheCell); 
     } 
    }   
} 
2

używam niestandardowej UITableViewCell i mam awarię zbyt na ios7

dniu iOS 6, to praca jak marzenie

UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview]; 
UITableView *table=(UITableView*)[cell superview]; 
NSIndexPath *path=[[sender superview] indexPathForCell:cell]; 

na ios7 ten kod powyżej katastrofie,

Ten kod działa na ios7 ale nie rozumiem dlaczego ...

UITableViewCell *cell = (UITableViewCell*)[[sender superview] superview]; 
UITableView *table = [[(UITableView*)[cell superview] superview] superview]; 
NSIndexPath *path=[[sender superview] indexPathForCell:cell]; 

więc używam edzio27 odpowiedź. Ustawiłem znacznik na moim przycisku.

+0

Oto rozwiązanie do pracy na iOS6 i iOS7 http://stackoverflow.com/a/19651825/1294448 –

Powiązane problemy