【问题标题】:deleteRowsAtIndexPaths on heavily-updated model crashes大量更新的模型崩溃时的 deleteRowsAtIndexPaths
【发布时间】:2013-04-05 08:17:21
【问题描述】:

视图控制器的数据模型会不断且积极地更新

经常调用 UItableview (storyTable) 来更新和反映迄今为止的更改。

(更改代码以仅反映单个 beginUpdates endUpdates) - 仍然崩溃

  [self.storyTable beginUpdates];
                if([deleteIndexPaths count]){
                    DLog(@"Table refresh delele rows :%@",deleteIndexPaths);

                    [self.storyTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationNone];


                }

                if ([addIndexPaths count]){
                    DLog(@"Table refresh add rows :%@",addIndexPaths);

                    [self.storyTable insertRowsAtIndexPaths:addIndexPaths withRowAnimation:UITableViewRowAnimationTop];

                }
                if ([changedIndexPaths count]){

                    DLog(@"Table refresh change rows :%@",changedIndexPaths);
                    [self.storyTable reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationFade ];

                }
                [self.storyTable endUpdates];
                //[self.storyTable endUpdates];
                tableIsAnimating = NO;

        }

不时崩溃并显示以下消息: (主要在 deleteRowsAtIndexPaths...)

* -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 0 节中的行数。包含在第 0 节中的行数 更新后的现有节(20)必须等于 更新前该部分中包含的行 (20),加号或减号 从该部分插入或删除的行数(0 插入, 10 已删除)加上或减去移入或移出的行数 该部分(0 移入,0 移出)。'

或:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试插入第 20 行 进入第 0 节,但在第 0 节之后只有 20 行 更新”

问题:如何添加一些保护代码来防止崩溃?

【问题讨论】:

  • 您有什么理由不想使用NSFetchedResultsControllerNSFetchedResultsControllerDelegate 来显示模型更改?你没有使用 Core Data 吗?

标签: iphone ios objective-c ipad uitableview


【解决方案1】:

我认为这里的问题是,当同时有删除、添加和更改时,你的 indexPaths 在第一个-endUpdates 之后都会出错。

你可以试试这个:

[self.storyTable beginUpdates];
if([deleteIndexPaths count]){
   [self.storyTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationNone];
}
if ([addIndexPaths count]){
   [self.storyTable insertRowsAtIndexPaths:addIndexPaths withRowAnimation:UITableViewRowAnimationTop];
}
if ([changedIndexPaths count]){
   [self.storyTable reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationFade ];
}
[self.storyTable endUpdates];

这样 indexPaths 在所有操作中都是有效的。

【讨论】:

  • 感谢您的帮助,我确实更改了代码(见上文),仍然有问题(应用程序崩溃)...
  • 很抱歉这没有解决您的问题。在那种情况下,我现在没有进一步的想法。
【解决方案2】:

我认为你的 tableview 改变不是因为你 deleterowsatindexpath 而是 reloaddata,换句话说,使用 deleterowateindexpath 不做基本的删除工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多