【发布时间】:2024-01-20 03:01:01
【问题描述】:
我正在删除一个表的几行。事实上,在这种情况下,所有可见的单元格实际上都被删除了。
这会导致可见帧下方的单元格在动画结束后变为可见,因为其上方的所有单元格现在都已消失。
问题来了,这个单元格也被删除了。所以我得到以下错误,我假设,因为 tableview 要我删除一个不在屏幕上的单元格。 (但会在动画期间/之后出现在屏幕上)。
2009-06-15 17:44:30.114 HSS[18175:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 4. The number of rows contained in an existing section after the update (0) 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 (0 inserted, 0 deleted).'
2009-06-15 17:44:30.117 HSS[18175:20b] Stack: (
807902715,
2429263419,
807986683,
811271572,
815059595,
815007323,
97367,
96328,
96282,
810768858,
807687328,
807683624,
839142449,
839142646,
814752238,
10968,
10822
)
那么在这种情况下我该怎么办?
导致异常的代码如下。请注意,我实际上在另一种方法中创建了一个名为“filteredTableGroups”的新数组。这是更新的模型。 “allTableGroups”是每个单元控制器。每个单元控制器都包含一个字典,用于填充其单元数据。我使用“filteredDataSet”键来确定单元格是否应保留在过滤表中。在 tableview 单元删除期间,我交换 tableGroups 以指向更新的模型。 (我创建的代码类似于 Matt Gallagher 和 Craig Hockenberry 的使用单元控制器控制单个单元的解决方案)
- (void)collapseVisableCells{
NSMutableArray *cellsToRemove = [NSMutableArray array];
for(NSArray *sections in self.allTableGroups){
for(ScheduleCellController *cellController in sections){
NSString *shouldDisplayString = (NSString*)[[cellController model] objectForKey:@"filteredDataSet"];
BOOL shouldDisplay = [shouldDisplayString boolValue];
if(!shouldDisplay){
UITableViewCell *theCell = [cellController myCell];
NSIndexPath *cellPath = [self.tableView indexPathForCell:theCell];
NSLog([cellPath description]);
if(cellPath!=nil)
[cellsToRemove addObject:cellPath];
}
}
}
[self.tableView beginUpdates];
tableGroups = self.filteredTableGroups;
[self.tableView deleteRowsAtIndexPaths:cellsToRemove withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
【问题讨论】:
标签: iphone cocoa-touch uikit