2013-06-30 37 views
5

Tôi có một UITableView với các ô tùy chỉnh. Trong mỗi UITableViewCell, có một UIButton. Tôi đang cố gắng để tìm ra tế bào nút được đặt trong khi nó được khai thác. Để thực hiện điều này, tôi đã làm điều này:Sụp đổ khi gọi đến 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); 

Tôi nghĩ điều này sẽ làm việc tốt, nhưng khi indexPathForCell được gọi, một ngoại lệ được ném.

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

Bất kỳ ý tưởng nào về những gì tôi đã làm sai? Cảm ơn!

Trả lời

6

Đây là vấn đề của bạn:

(UITableViewCell *)[senderButton superview] 

Phải là:

(UITableViewCell *)[[senderButton superview] superview] 

Bởi vì giám sát của nút không phải là ô, là contentView mà phần phụ của ô.

+0

Đã hoạt động! Cảm ơn! – user2535943

+0

lưu ý: thao tác này sẽ hoạt động trên iOS7 nhưng không hoạt động trên iOS6! –

+0

bạn tiết kiệm thời gian của tôi, nhiều thanx. – Vigor

2

Tại sao bạn không đặt thẻ cho mỗi nút trong phương pháp cellForRowAtIndexPath của bạn:

button.tag = indexPath.row; 

và sau đó trong phương pháp của bạn, bạn có:

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

Hoặc bạn có thể đặt thẻ cho mỗi nút theo đề nghị của edzio27 hoặc bạn có thể thử sử dụng introspection như hình dưới đây:

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

tôi sử dụng tùy chỉnh UITableViewCell và tôi có một vụ tai nạn quá trên iOS7

On ios 6, nó làm việc như một nét duyên dáng

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

trên iOS7, mã này trên sụp đổ,

Mã này hoạt động trên iOS7 nhưng tôi không hiểu tại sao ...

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

Vì vậy, tôi sử dụng edzio27 câu trả lời. Tôi đặt một thẻ trên nút của tôi.

+0

Đây là giải pháp để hoạt động cả trên iOS6 & iOS7 http://stackoverflow.com/a/19651825/1294448 –

Các vấn đề liên quan