I am performing a deletion of several rows of a table. In fact, on this occasion all visible cells are in fact deleted.
This causes the cell immediately below the visible frame to become visible after the animation, since all cells above it are now gone.
Here is the issue, this cell is also deleted. So I get the following error, I assume, because the tableview wants me to delete a cell that isn't on screen. (but WILL be on screen during/after the animation).
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
)
So what do I do in this situation?
Code that causes exception follows. Note that I actually create a new array called "filteredTableGroups" in another method. This is the updated model. "allTableGroups" is every Cell Controller. Each cell controller contains a dictionary used to populate it's cells data. I use a key, "filteredDataSet", to determine whether a cell should remain in the filtered table. During the tableview cell deletion, I swap tableGroups to point to the updated model. (I am created my code similar to Matt Gallagher and Craig Hockenberry's solution of using Cell Controller's to control individual cells)
- (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];
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…