2012-10-31 23 views
11

Cuối cùng tôi đã theo dõi một vụ tai nạn kỳ lạ. Nó được gây ra bởi tableView:commitEditingStyle:forRowAtIndexPath: được gọi với một chỉ số nil indexPath. Nhưng làm thế nào nó có thể được?Làm thế nào có thể tableView: commitEditingStyle: forRowAtIndexPath: được gọi với một nil indexPath?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // DEBUG 
     if (indexPath == nil) { 
      NSLog(@"Deleting row at nil indexPath in %@", self); 
     } 
     [self deleteItemAtIndexPath:indexPath fromTableView:tableView]; 
    } 
} 

Đây là stack trace:

6 XXX  -[ListViewController tableView:commitEditingStyle:forRowAtIndexPath:] (ListViewController.m:427) 
7 UIKit -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 85 
8 UIKit -[UIApplication sendAction:to:from:forEvent:] + 73 
9 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 31 
10 UIKit -[UIControl sendAction:to:forEvent:] + 45 
11 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503 
12 UIKit -[UIApplication sendAction:to:from:forEvent:] + 73 
13 UIKit -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 31 
14 UIKit -[UIControl sendAction:to:forEvent:] + 45 
15 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503 
16 UIKit -[UIControl touchesEnded:withEvent:] + 489 
17 UIKit -[UIWindow _sendTouchesForEvent:] + 525 
18 UIKit -[UIApplication sendEvent:] + 381 
19 UIKit _UIApplicationHandleEvent + 6155 
20 GraphicsServices _PurpleEventCallback + 591 
21 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
22 CoreFoundation __CFRunLoopDoSources0 + 213 
23 CoreFoundation __CFRunLoopRun + 647 
24 CoreFoundation CFRunLoopRunSpecific + 357 
25 CoreFoundation CFRunLoopRunInMode + 105 
26 GraphicsServices GSEventRunModal + 75 
27 UIKit UIApplicationMain + 1121 
28 XXX  main (main.m:16) 
29 XXX  start + 40 
+0

Bạn có một stack trace cho thấy nơi các cuộc gọi đến từ đâu? – rmaddy

+0

Tôi đã thêm dấu vết ngăn xếp. – an0

+0

Tôi có cùng lỗi IndexPath nhưng với một dấu vết ngăn xếp khác. Bạn đã quản lý để khắc phục sự cố chưa? – sergtk

Trả lời

-1

Trường hợp ngoại lệ là do sự hiện diện của dữ liệu ở chỉ số nhưng indexPath đã được xóa. Bạn cần xóa đối tượng mảng tại chỉ mục.

Hãy thử như sau:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if (editingStyle == UITableViewCellEditingStyleDelete) 
     { 
     [self deleteItemAtIndexPath:indexPath fromTableView:tableView]; 
     NSMutableArray * tempArray = [yourDataArray mutableCopy]; 
     [tempArray removeObjectAtIndex: indexPath.row]; 
     yourDataArray = tempArray; 
     [tableView reloadData]; 
     } 
    } 
Các vấn đề liên quan