ios - 无效更新 : invalid number of rows in section 0 UITableView

标签 ios uitableview datasource

我知道这个问题已经被问过 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];

最佳答案

我发现它与 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];

关于ios - 无效更新 : invalid number of rows in section 0 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914619/

相关文章:

grails - 如何在 Grails 2.4.X 中获取自定义数据源引用?

ios - UIView.animateWithDuration swift 3

ios - 无法向我的应用程序添加新的 iOS 包标识符 - LinkedIn

ios - 在 didSelectRowAtIndexPath 中更改自定义单元格的标签高度

ios - 我如何使用在 main.storyboard 中创建的原型(prototype)单元格数组填充正确的 swreveal View Controller

spring - 通过 org.springframework.orm.hibernate4.LocalSessionFactoryBean 创建 HibernateTemplate

Grails 2 服务中的多个动态数据源

ios - 从 iPhone 调用 Azure IoT 中心

ios - 更改整个应用程序中的字体大小

ios - TableView : how to get a string from selected row?