1.以前寫代碼的時候,自己常常在cell中傳入一個NSIndexPath來標(biāo)識這個cell所在的IndexPath,例如如下
但是在刪除cell的時候出現(xiàn)了bug崩潰
經(jīng)過我的查看,當(dāng)你刪除某一個cell的時候,還在屏幕內(nèi)的cell已經(jīng)記錄的數(shù)據(jù)是不會刷新的,也就是說cell.indexPath是不會刷新的. 那該怎么辦呢?
在查資料之后
我決定這樣做:
沒錯, 在刪除的時候把整個cell傳回來,在調(diào)用tableview自己的方法來獲取indexPath這是絕對不會錯了,接下來就好辦了 哈哈...
2.還有關(guān)于有多個section的cell的刪除
這個實際上在某個section的cell被刪除完畢的時候,一定要同時把section本身刪除這樣就不會崩潰了...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *a = _data[indexPath.section];
[a removeObjectAtIndex:indexPath.row];
if (a.count == 0) { // 要根據(jù)情況直接刪除section或者僅僅刪除row
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
} else {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}