gpt4 book ai didi

ios - 从 UITableView 中删除所有行

转载 作者:行者123 更新时间:2023-11-29 01:46:52 25 4
gpt4 key购买 nike

我一直遇到断言失败,但我不确定如何解决。

最初我做了一个概念验证应用程序以了解其功能并在其中包含以下代码:

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 个 NSIndexPath。谁能指出我做错了什么的正确方向?

   - (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];

}
}

最佳答案

[myArray removeAllObjects];
[tableView reloadData];

关于ios - 从 UITableView 中删除所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com