insertRowsAtIndexPaths 和deleteRowsAtIndexPaths 同numberOfRowsInSection的關系
如果不處理好這個關系,大概所有的問題都是這樣的:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 6. 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 (9), plus or minus the number of rows inserted or deleted from that section (0 inserted, 3 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 4 which only contains 0 rows before the update'
大楖是說,numberOfRowsInSection 返回的數字和 insert / delete 後,section 所包含的行數不一致。
在 tableView 的 endUpdates 方法之前,應該對標志變量做處理。endUpdates 後,會重新刷新 tableView ,numberOfRowsInSection 會重新被調用。
解決方法有兩種:
1,如上所說處理好標志變量:
if ([wwtableview numberOfRowsInSection:4]>2) {}
2,tableview beginUpdates之前的行數對應的是前一個數據源,開始reloadcell時tableview並不知道數據源發生了變化
[wwtableviewreloadData]