先描述下錯誤:
Assertion failure in -[UITableView _endCellAnimationsWithContext:]---錯誤
或者看crash日志錯誤:
'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’
原因:
1.在調(diào)用deleteRowsAtIndexPaths:方法前,要確保數(shù)據(jù)為最新朽色。也就是說邻吞,先將要刪除的數(shù)據(jù)從數(shù)據(jù)源中刪除。
2.分組和分組中行數(shù)是變動的葫男,不能寫成死的抱冷!
3.如果是分組踏志,你會發(fā)現(xiàn)很怪的現(xiàn)象:當(dāng)一個分組中麸粮,有多條數(shù)據(jù)時愚争,你刪除其中一條斑响,正確;當(dāng)一個分組中胚泌,你要刪除唯一的一條時菌瘪,仍然會報出如上的錯誤须眷!
本人鱼响,就是百思不得其解鸣剪。反復(fù)調(diào)試,數(shù)據(jù)已經(jīng)保持最新了的丈积。
在網(wǎng)上搜了很多筐骇,卻沒有滿意答案。
靈感閃過江滨!~~~~
刪除某個分組中的最后一條數(shù)據(jù)時铛纬,分組數(shù),和行數(shù)都要變唬滑。這時候告唆,只調(diào)用了deleteRowsAtIndexPaths方法莫秆。也就是說,只對行數(shù)進行了操作悔详,但是沒有對變動的分組進行操作!
查看幫助API惹挟,找到這么一個方法:deleteSections:方法茄螃!
加上去,在刪除某個分組中最后一條記錄時连锯,將該分組也刪除归苍!
搞定!搞定运怖!
[tableView beginUpdates];
if(newCount<=0) {
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
}
[tableViewdeleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
或者拼弃,考慮下是否在最后一個分組的條件下,需要變換分組的個數(shù)摇展,如果沒有必要的話吻氧,就可以保留 section 的個數(shù)。
參考:https://blog.csdn.net/oqqquzi1234567/article/details/8946583