Xcode Error - iOS 7 Swipe Left to Reveal Delete Function

1

Okay, first I am an absolute beginner and my question actually deals with my first project. I'm taking it slow and making sure I understand everything. Thanks for your patience in advance.

I've followed an Xcode tutorial online on creating a To-Do List app. I was successful at creating this as the tutorial explained. However, I want to move on and customize the app now. Specifically I wanted to add a "swipe left to reveal delete button" function because the other did not have it. I found a code snippet for this online. I'm pasting it below.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    XYZTopList *tappedItem = [self.XYZTopList objectAtIndex:indexPath.row];
    tappedItem.completed = !tappedItem.completed;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

However, whenever I run my program in the simulator, the program runs beautifully even to the point of swiping left to reveal the red delete box. But, when I click delete...I get this error message. I searched online to the best of my ability and found some things about possibly having some connection errors due to buttons and outlets. However, in this error message it describes an unequal amount of rows before and after an update (the deletion).

[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:1330
2014-01-07 14:04:31.307 Top That[3281:70b] *** 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 (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 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                      0x0173c5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bf8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0173c448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0109ffee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x002fe85d -[UITableView _endCellAnimationsWithContext:] + 13402
    5   UIKit                               0x0030e550 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 337
    6   UIKit                               0x0030e5cb -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 55
    7   Top That                            0x00002821 -[XYZTopListViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 193
    8   UIKit                               0x0031dba3 -[UITableView animateDeletionOfRowWithCell:] + 107
    9   UIKit                               0x0049d695 -[UITableViewCell _swipeDeleteButtonPushed] + 70
    10  libobjc.A.dylib                     0x014d1874 -[NSObject performSelector:withObject:withObject:] + 77
    11  UIKit                               0x0022f0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    12  UIKit                               0x0022f04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    13  UIKit                               0x003270c1 -[UIControl sendAction:to:forEvent:] + 66
    14  UIKit                               0x00327484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    15  UIKit                               0x00326733 -[UIControl touchesEnded:withEvent:] + 641
    16  UIKit                               0x005a1c7f _UIGestureRecognizerUpdate + 7166
    17  UIKit                               0x0026c19a -[UIWindow _sendGesturesForEvent:] + 1291
    18  UIKit                               0x0026d0ba -[UIWindow sendEvent:] + 1030
    19  UIKit                               0x00240e86 -[UIApplication sendEvent:] + 242
    20  UIKit                               0x0022b18f _UIApplicationHandleEventQueue + 11421
    21  CoreFoundation                      0x016c583f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    22  CoreFoundation                      0x016c51cb __CFRunLoopDoSources0 + 235
    23  CoreFoundation                      0x016e229e __CFRunLoopRun + 910
    24  CoreFoundation                      0x016e1ac3 CFRunLoopRunSpecific + 467
    25  CoreFoundation                      0x016e18db CFRunLoopRunInMode + 123
    26  GraphicsServices                    0x036e19e2 GSEventRunModal + 192
    27  GraphicsServices                    0x036e1809 GSEventRun + 104
    28  UIKit                               0x0022dd3b UIApplicationMain + 1225
    29  Top That                            0x000035ed main + 141
    30  libdyld.dylib                       0x01d7a70d start + 1
    31  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Basically, I have zero clue with these error messages and debugging. Thanks for any help in advance, as well as, for using terms that a beginner (eager to advance) can understand.

ios7
xcode5
asked on Stack Overflow Jan 7, 2014 by davidrayowens

1 Answer

1

The problem is described in a little more detail where it says:

'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Specifically, you have told the table that a cell was removed, but when it gets the table contents it has the same number it did before.

Make sure that you are removing an object from self.XYZTopList when deleting. With the code provided it is a little hard to see where or how you would do that, but here are some related issues that go into more detail:

Core Data example, Invalid update: invalid number of rows in section 0

Invalid update: invalid number of rows in section 0 UITableView

answered on Stack Overflow Jan 7, 2014 by James • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0