deleteRowsAtIndexPaths的NSInternalInconsistencyException异常

来源:互联网 发布:王凯整容了吗 知乎 编辑:程序博客网 时间:2024/06/07 10:05

在表视图的行删除操作中,用deleteRowsAtIndexPaths方法删除一个indexPath数组时,抛出一个错误NSInternalInconsistencyException。将出错前后的代码摘录如下。

[self beginUpdates];

[self deleteRowsAtIndexPaths:@[[self indexPathForCell:cell]]withRowAnimation:UITableViewRowAnimationFade];

[self endUpdates];

[self.dsItems removeObject:_originalShpItem];

 

使用断点追踪时,发现抛错代码在endUpdates方法上。

错误信息如下:

** Terminating app due to uncaught exception'NSInternalInconsistencyException', reason: 'Unable to resolve row for indexpath:  2 indexes [0, 1]'

 

先确定一点,这个错是在对于uitableview的机制使用上不熟悉导致的,一定不是uitableview的自生问题,别动不动怀疑ios的bug。

有人这么建议的:

The table view data source needs to be consistent with yourinsert/delete calls. Set a break point in numberOfRowsInSection to assure thatthe number of rows in a section is correct after the insert/delete rows.

表视图的数据源需要和你插入或者删除的操作保持一致。在numberOfRowsInSection上做一个断点,确认插入或者删除记录后结果集总数要和deleteRowsAtIndexPaths操作一致。

通过设置numberOfRowsInSection断点,可以很清楚看到deleteRowsAtIndexPaths的执行流程,它在endUpdates之前,是要调用numberOfRowsInSection的,从而得到删除cell后的记录数。

如果这里没有beginUpdates和endUpdates的封装,那么必须在deleteRowsAtIndexPaths之前就将datasource进行删除。否则就会报这个错误。

 

至于有人提到,不用deleteRowsAtIndexPaths操作,而是使用reloaddata,全屏
原创粉丝点击