无效更新:第 0 节 UITableView 中的行数无效

Posted

技术标签:

【中文标题】无效更新:第 0 节 UITableView 中的行数无效【英文标题】:Invalid update: invalid number of rows in section 0 UITableView 【发布时间】:2013-07-28 16:29:25 【问题描述】:

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

【问题讨论】:

这听起来像是逻辑很糟糕。例如,您为什么要自己调用 UITableViewDelegate 方法?回到绘图板上,弄清楚你想要做什么。也许在问题中概述您的目标会很有用。比如,你展示了什么样的数据?为什么要删除带有复选标记的某些项目? 原来来自这个问题的第一个答案:***.com/questions/6227168/… 这与您的问题无关,但是一旦您的同步错误结束,您将遇到此错误:如果您有两个标记的相邻行,则将跳过第二行。这是因为您从前到后迭代:删除索引处的对象会将其邻居移到其位置,但循环无论如何都会增加索引i,因此您跳过下一个邻居。向后迭代数组以解决此问题。 您正试图让用户检查要删除的单元格,然后立即删除它们。知道了。你的问题是你正在从循环中间的cellArray 中删除项目。这意味着像cellForRowAtIndexPath 这样的东西会与您对它们的要求混淆(因为它们取决于该数组的计数)。因此,您可以在此处更改代码以在循环之后从cellArray 中删除内容,就像您对deleteRowsAtIndexPaths 所做的那样。但同样,我会找到另一种不依赖于与 tableviewdelegate 交互的方法。 查看我发布的新代码。它有点接近我需要的东西,但它仍然会因同样的错误而崩溃。我也知道我必须向后枚举,但我还没有达到那一点。从现在开始我应该怎么做? 【参考方案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];

【讨论】:

确保您添加了代码以从数据源以及 begin/endUpdates 中删除 indexPath,而不仅仅是 tableView deleteRows。

以上是关于无效更新:第 0 节 UITableView 中的行数无效的主要内容,如果未能解决你的问题,请参考以下文章

删除 UITableView 中的行时出错

无效更新:第 0 节中的无效行数。更新后现有节中包含的行数 (3)

tableView 编辑委托方法不起作用(无效更新:第 0 节中的行数无效)

无效更新:第 0 节中的无效行数以 NSException 类型的未捕获异常终止

删除行:错误“无效更新:第 0 节中的行数无效”

无效更新:第 0 节中的项目数无效。