Недопустимое обновление: недопустимое количество строк в разделе 0 UITableView

Я знаю, что это было задано до 50 раз, но я просмотрел все остальные ответы и не нашел ничего, что могло бы исправить мою проблему.

В любом случае это мой код:

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];

И когда я выполняю этот код, я получаю этот журнал сбоев:

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).'

Кто-нибудь видит, что я делаю неправильно?

Новый код:

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];

Ответ 1

Я понял, что это связано с numberOfRowsInSection. Я не обновлял свои nsuserdefaults с обновленным источником данных, поэтому я сделал это сейчас, и он работает отлично. Вот мой обновленный код, кстати:

//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];

Ответ 2

Вам необходимо добавить следующее до и после удаления строки:

[theTableView beginUpdates];
// your code goes here
[theTableView endUpdates];