deleteRowsAtIndexPaths的NSInternalInconsistencyException異常

在表視圖的行刪除操作中,用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,全屏
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章