我一直遇到断言失败,但我不确定如何解决。
最初,我做了一个概念证明应用程序,以了解它的功能并在其中包含此代码:
if (self.arrNonATIResults.count > 0) {
NSMutableArray* arrIndexPaths = [NSMutableArray array];
for(int i=0; i<[self.arrNonATIResults count]; i++) {
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[arrIndexPaths addObject:anIndexPath];
}
[self.accountTable beginUpdates];
[self.accountTable deleteRowsAtIndexPaths:arrIndexPaths withRowAnimation:UITableViewRowAnimationTop];
self.arrNonATIResults = [NSMutableArray array];
[self.accountTable endUpdates];
}
工作得很好。将该代码移到我的应用程序中我遇到了这个断言失败:
Invalid update: invalid number of rows in section 0. 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 (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
在这里我感到困惑,self.arrNonATIResults(表数据源)包含两个对象(它是硬编码的) - 所以表只有两行。错误消息中的第3行来自哪里?我确实读过如果你删除所有行,那么你也必须删除该部分。这是错误消息中的第三项吗?
所以我重写了我的初始代码,但是这仍然不行,因为arrIndexPath数组只包含2个NSIndexPaths。任何人都可以指出我正确的方向,我做错了吗?
- (void) clearNonATITable {
if (self.arrNonATIResults.count > 0) {
NSMutableArray* arrIndexPaths = [NSMutableArray array];
for(int i=0; i<[self.arrNonATIResults count]; i++) {
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[arrIndexPaths addObject:anIndexPath];
}
[self.accountTable beginUpdates];
for (int i=0; i<arrIndexPaths.count; i++) {
NSIndexPath* thisIndexPath = [arrIndexPaths objectAtIndex:i];
if (self.arrNonATIResults.count > 0) {
[self.accountTable deleteRowsAtIndexPaths:[NSArray arrayWithObjects:thisIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[self.arrNonATIResults removeObjectAtIndex:0];
} else {
NSIndexSet* setIndexSections = [[NSIndexSet alloc] initWithIndex:thisIndexPath];
[self.accountTable deleteSections:setIndexSections withRowAnimation:UITableViewRowAnimationFade];
}
}
[self.accountTable endUpdates];
}
}
答案 0 :(得分:1)
[myArray removeAllObjects];
[tableView reloadData];
答案 1 :(得分:0)
问题是应用程序在这一个tableview中使用了3个数据源 - 数据源将根据段控件选择而改变。我只需要确定它是哪个来源并使用它来计算要删除的NSIndexPaths的数量。