Invalid update: invalid number of rows in section 0

Luca Briganti picture Luca Briganti · Sep 4, 2014 · Viewed 11.9k times · Source

i have problem when i try delete or add object a the list

errror:

2014-09-04 10:59:03.815 DeleteListTest[2781:60b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1368
2014-09-04 10:59:03.856 DeleteListTest[2781:60b] *** 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 (9) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
    0   CoreFoundation                      0x01b581e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018d78e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01b58048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x014b74de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x0066ff63 -[UITableView _endCellAnimationsWithContext:] + 13402
    5   UIKit                               0x0067fcea -[UITableView endUpdatesWithContext:] + 51
    6   UIKit                               0x0067fd18 -[UITableView endUpdates] + 41
    7   DeleteListTest                      0x00002ece -[MasterViewController leaveEditMode] + 270
    8   DeleteListTest                      0x0000301a -[MasterViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 202
    9   UIKit                               0x006916a3 -[UITableView animateDeletionOfRowWithCell:] + 107
    10  UIKit                               0x0081a595 -[UITableViewCell _swipeDeleteButtonPushed] + 70
    11  libobjc.A.dylib                     0x018e9880 -[NSObject performSelector:withObject:withObject:] + 77
    12  UIKit                               0x005993b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    13  UIKit                               0x00599345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    14  UIKit                               0x0069abd1 -[UIControl sendAction:to:forEvent:] + 66
    15  UIKit                               0x0069afc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    16  UIKit                               0x0069a243 -[UIControl touchesEnded:withEvent:] + 641
    17  UIKit                               0x0092f2e3 _UIGestureRecognizerUpdate + 7166
    18  UIKit                               0x005d8a5a -[UIWindow _sendGesturesForEvent:] + 1291
    19  UIKit                               0x005d9971 -[UIWindow sendEvent:] + 1021
    20  UIKit                               0x005ab5f2 -[UIApplication sendEvent:] + 242
    21  UIKit                               0x00595353 _UIApplicationHandleEventQueue + 11455
    22  CoreFoundation                      0x01ae177f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    23  CoreFoundation                      0x01ae110b __CFRunLoopDoSources0 + 235
    24  CoreFoundation                      0x01afe1ae __CFRunLoopRun + 910
    25  CoreFoundation                      0x01afd9d3 CFRunLoopRunSpecific + 467
    26  CoreFoundation                      0x01afd7eb CFRunLoopRunInMode + 123
    27  GraphicsServices                    0x03a0a5ee GSEventRunModal + 192
    28  GraphicsServices                    0x03a0a42b GSEventRun + 104
    29  UIKit                               0x00597f9b UIApplicationMain + 1225
    30  DeleteListTest                      0x00004a4d main + 141
    31  libdyld.dylib                       0x0219f701 start + 1
    32  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

this is the delete method if(editingStyle == UITableViewCellEditingStyleDelete){ [tableTitles removeObjectAtIndex:[indexPath row]]; [self leaveEditMode]; [[self tableView] reloadData]; someone can help me ? thx u :)

Answer

trojanfoe picture trojanfoe · Sep 4, 2014

I believe you are missing the call to:

[self.tableView deleteRowsAtIndexPaths:indexPath
                      withRowAnimation:UITableViewRowAnimationFade];

and you don't need the reloadData call:

if (editingStyle == UITableViewCellEditingStyleDelete) {
    [tableTitles removeObjectAtIndex:[indexPath row]];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    [self leaveEditMode];

(this will also look better as it will give visual feedback to the user; unlike reloadData).