ios - 从 UITableView 中删除所有行

标签 ios uitableview cocoa-touch

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

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

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/

相关文章:

ios - UITableViewCell 类中的预期声明

iphone - ios5 UISearchBar 过滤 TableView 导致空单元格断言错误

ios - 具有 UINavigationController 的应用程序中的持久 header

ios - UITableView:在构建表格后更改一个单元格的高度约束?

ios - 未显示 Collection View ,从未调用 cellForItemAtIndexPath

iphone - iphone 中的日期选择器

ios - 是否可以使用 UITableViewStylePlain 禁用 UITableView 中的 float 标题?

objective-c - 从不同的 View Controller 访问属性

ios - 为 iPhone 和 arm64 构建 pjsip 的 libyuv

ios - 在 UIView 中居中对齐 CAShape 图层