从 UITableView 中删除所有行

Posted

技术标签:

【中文标题】从 UITableView 中删除所有行【英文标题】:Deleting all rows from UITableView 【发布时间】:2015-08-04 13:20:24 【问题描述】:

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

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

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(表数据源)包含两个对象(它是硬编码的) - 所以表只有两行。错误消息中的第三行来自哪里?我确实读过,如果您删除所有行,那么您也必须删除该部分。这是错误消息中的第三项吗?

所以我重写了我的初始代码,但这仍然不应该工作,因为 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];

        
    

【问题讨论】:

【参考方案1】:
[myArray removeAllObjects];
[tableView reloadData];

【讨论】:

谢谢,我解决了。我正在对我没有编写的现有应用程序进行增强,并且没有意识到他们正在为表使用 3 个数据源。只需要检查他们使用的是哪个源,它的计数并将相同数量的 NSIndexPaths 添加到我的数组中。【参考方案2】:

问题是应用程序为这个表视图使用了 3 个数据源 - 数据源会根据段控制选择而改变。我只需要确定它是哪个源并将它的计数用于要删除的 NSIndexPaths 的数量。

【讨论】:

以上是关于从 UITableView 中删除所有行的主要内容,如果未能解决你的问题,请参考以下文章

在运行时从静态 UITableView 中删除行

iOS - 从 UITableView 中删除行没有动画

从 UITableView 中删除行会更改存储在其他行中的信息

从 UITableView NSMutableArray 和 ManagedObjectContext 中删除行

使用“插入”行初始化 UITableView

删除行 UITableView 索引问题