Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid update: invalid number of rows in section 0 UITableView

I know this has been asked before 50 times but I have looked through all the other answers and didn't find anything that could fix my issue.

Anyway this is my code:

NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
        for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
            NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
            if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
                [cellIndicesToBeDeleted addObject:p];
                [self.cellArray removeObjectAtIndex:i];
            }
        }
        [thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
        [cellIndicesToBeDeleted release];

And when I execute this code I get this crash log:

Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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).'

Does anyone see what I am doing wrong?

Newish code:

NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
        NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
        for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
            NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
            if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
                [cellIndicesToBeDeleted addObject:p];
                [mutableIndexSet addIndex:p.row];
            }
        }
        [self.cellArray removeObjectsAtIndexes:mutableIndexSet];
        [thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
        [cellIndicesToBeDeleted release];
        [mutableIndexSet release];
like image 752
SimplyKiwi Avatar asked Jul 29 '13 01:07

SimplyKiwi


2 Answers

I figured it out it had to do with the numberOfRowsInSection. I wasn't updating my nsuserdefaults with the updated datasource so I did that now and it works perfectly. Here is my updated code by the way:

//Code to delete rows
        NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
        NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
        for (int i = [thetableView numberOfRowsInSection:0] - 1; i >= 0; i--) {
            NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
            if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
                [cellIndicesToBeDeleted addObject:p];
                [mutableIndexSet addIndex:p.row];
            }
        }
        [self.cellArray removeObjectsAtIndexes:mutableIndexSet];
        [[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
        [thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
        [cellIndicesToBeDeleted release];
        [mutableIndexSet release];
like image 77
SimplyKiwi Avatar answered Sep 30 '22 23:09

SimplyKiwi


You need to add the following before and after you remove the row:

[theTableView beginUpdates];
// your code goes here
[theTableView endUpdates];
like image 24
Bay Phillips Avatar answered Oct 01 '22 00:10

Bay Phillips