2010-11-15 9 views
9

Ứng dụng của tôi gặp sự cố khi tôi xóa hàng khỏi bảng. Đây là nguồn của tôi, nơi phát hiện lỗi và theo dõi ngăn xếp. Thanx!sự cố trên deleteRowsAtIndexPaths

//delete row from database 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"\ncommitEditingStyle"); 

    //delete user from Users table with specified ID 
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    { 

     //Get the object to delete from the array. 
     [dbManager open: @DB_FILE_NAME];  

     //get userID from array usersIDList[row] = userID! 
     NSNumber *usersID = [usersIDList objectAtIndex: [indexPath row]]; 
     [dbManager deleteUserWithID: [usersID intValue] table: @TABLE_USERS fieldName: @"UserID" ]; 

     [dbManager close]; 

     //Delete the object from the table. 
     [self.tableView deleteRowsAtIndexPaths: 
     [NSArray arrayWithObject: indexPath] 
     withRowAnimation: UITableViewRowAnimationFade]; 
    } 
} 

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    NSLog(@"\ndeleteRowsAtIndexPaths"); 
} 

[Session started at 2010-11-15 18:01:23 +0200.] 
2010-11-15 18:01:25.052 PhoneBook[12397:207] ******* Accessibility Status Changed: On 
2010-11-15 18:01:25.101 PhoneBook[12397:207] ********** Loading AX for: com.yourcompany.PhoneBook ************ 
2010-11-15 18:01:30.060 PhoneBook[12397:207] 
deleteBtnUserClick 
2010-11-15 18:01:31.878 PhoneBook[12397:207] 
commitEditingStyle 
2010-11-15 18:01:31.881 PhoneBook[12397:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920 
2010-11-15 18:01:31.883 PhoneBook[12397:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x02510b99 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x0266040e objc_exception_throw + 47 
    2 CoreFoundation      0x024c9238 +[NSException raise:format:arguments:] + 136 
    3 Foundation       0x000b8e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
    4 UIKit        0x00338d37 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8719 
    5 UIKit        0x00328511 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56 
    6 UIKit        0x0aaca8fc -[UITableViewAccessibility(Accessibility) deleteRowsAtIndexPaths:withRowAnimation:] + 74 
    7 PhoneBook       0x00003491 -[RootViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 392 
    8 UIKit        0x0032596d -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101 
    9 UIKit        0x0aacde92 -[UITableViewCellAccessibility(SafeCategory) deleteConfirmationControlWasClicked:] + 62 
    10 UIKit        0x002be7f8 -[UIApplication sendAction:to:from:forEvent:] + 119 
    11 UIKit        0x00349de0 -[UIControl sendAction:to:forEvent:] + 67 
    12 UIKit        0x0034c262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 
    13 UIKit        0x0034ae0f -[UIControl touchesEnded:withEvent:] + 458 
    14 UIKit        0x002e23d0 -[UIWindow _sendTouchesForEvent:] + 567 
    15 UIKit        0x002c3cb4 -[UIApplication sendEvent:] + 447 
    16 UIKit        0x002c89bf _UIApplicationHandleEvent + 7672 
    17 GraphicsServices     0x02d6c822 PurpleEventCallback + 1550 
    18 CoreFoundation      0x024f1ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    19 CoreFoundation      0x02452807 __CFRunLoopDoSource1 + 215 
    20 CoreFoundation      0x0244fa93 __CFRunLoopRun + 979 
    21 CoreFoundation      0x0244f350 CFRunLoopRunSpecific + 208 
    22 CoreFoundation      0x0244f271 CFRunLoopRunInMode + 97 
    23 GraphicsServices     0x02d6b00c GSEventRunModal + 217 
    24 GraphicsServices     0x02d6b0d1 GSEventRun + 115 
    25 UIKit        0x002ccaf2 UIApplicationMain + 1160 
    26 PhoneBook       0x000026ec main + 102 
    27 PhoneBook       0x0000267d start + 53 
) 
terminate called after throwing an instance of 'NSException' 

Trả lời

31

Tôi đã thấy điều này trước đây và chắc chắn bạn đã quên cập nhật nguồn dữ liệu lấp đầy bảng. Nhưng phần mã đó bị thiếu. Phương pháp khiến đây là

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return ; 
} 

5

Khá rõ ràng từ các thông báo lỗi:

'cập nhật không hợp lệ: số không hợp lệ của hàng trong phần 0. Số hàng chứa trong một phần hiện có sau bản cập nhật (6) phải bằng số hàng có trong phần trước bản cập nhật (6), hoặc trừ số hàng được chèn hoặc bị xóa khỏi phần đó (0 đã chèn, 1 đã bị xóa). '

Đối tượng bạn đang cố gắng xóa không bị xóa trong nguồn dữ liệu của bạn.
Nếu tôi giả định rằng UITableViewDataSource của bạn cũng sử dụng dbManager, bạn phải sửa dbManager để thực sự xóa đối tượng.

32

Vâng, vấn đề là trong phương pháp numberOfRowsInSection Vì vậy, để rời khỏi rắc rối, bạn nên:

  1. Trong chức năng commitEditingStyle dữ liệu xóa từ mảng, cơ sở dữ liệu của bạn, vv
  2. Decrement đếm hàng hiện tại của bạn.
  3. [tableView beginUpdates];
  4. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];

  5. [tableView endUpdates];

Và tất cả được tạp chí OK!

+3

bạn có cần bọc các cập nhật [bắt đầu và kết thúc] không? – alexzg

2

Tôi đã nhận lỗi cùng và lý do là tôi xóa dòng cuối cùng trong bảng và thực hiện numberOfSectionsInTableView đếm trở lại rowsArray của tôi mà là 0 ..

thay đổi để tối thiểu là 1 giải quyết vụ án

4

mới nhất Swift Giải pháp

Nếu bạn định loại bỏ hàng cuối cùng trong phần, bạn nên xóa toàn bộ phần thay vì hàng. Để làm điều đó:

tableView.beginUpdates() 
    self.dataArray.removeAtIndex(index.row) 
    if tableView.numberOfRowsInSection(index.section) == 1 { 
    tableView.deleteSections(NSIndexSet(index: index.section), withRowAnimation: .Automatic) 
    } 
    else { 
    tableView.deleteRowsAtIndexPaths([index], withRowAnimation: .Fade) 
    } 
    tableView.endUpdates()