Andy
Andy

Reputation: 479

Adding rows to a UITableView - NSInternalInconsistency Error

Okay I know there are tons of posts on SO about this - that is how I built my solution... But I cannot seem to get this working.

The idea is when a user touches the "Theme" row in my UITableView (Section 1 Row 0) I want it to expand to show all 5 themes we have at hand... This is my solution now :

if(indexPath.section == 1 && indexPath.row == 0){
    [[_dataArray objectAtIndex:1] setObject:@[@"Theme", @"Cool Blue", @"Clean Black", @"Lime Green", @"Deep Orange", @"Dark Red"] forKey:@"data"];

    NSMutableArray *indexPaths = [NSMutableArray array];

    for (int i = 0; i < 6; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:indexPath.row+i inSection:indexPath.section]];
    }

    NSLog(@"The paths are %@", indexPaths);

    [_menuTable beginUpdates];
    [_menuTable insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
    [_menuTable endUpdates];
}

And this is the error I am getting :

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. 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 (1), plus or minus the number of rows inserted or deleted from that section (6 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Any ideas as to why that is happening? I have all the delegates set up and I am (I think) using the right logic here....

Thanks

Upvotes: 0

Views: 78

Answers (1)

Putz1103
Putz1103

Reputation: 6211

That error is telling you that in the beginning of your updates you have one cell. At the end of the update you have 7 cells (1 + the 6 you add). But when it calls your numberOfRowsInSection function you are returning 6 to it after the animation. So there is an inconsistency. It doesn't know what you want so it crashes.

Make sure you aren't adding your initial cell to the table even though it's already on the table.

Upvotes: 1

Related Questions