关于cell删除

2016-12-09  本文已影响29人  今年27

1.以前写代码的时候,自己常常在cell中传入一个NSIndexPath来标识这个cell所在的IndexPath,例如如下

但是在删除cell的时候出现了bug崩溃

经过我的查看,当你删除某一个cell的时候,还在屏幕内的cell已经记录的数据是不会刷新的,也就是说cell.indexPath是不会刷新的. 那该怎么办呢?

在查资料之后

我决定这样做:

没错, 在删除的时候把整个cell传回来,在调用tableview自己的方法来获取indexPath这是绝对不会错了,接下来就好办了 哈哈...

2.还有关于有多个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) { // 要根据情况直接删除section或者仅仅删除row

[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

} else {

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

}

}

上一篇下一篇

猜你喜欢

热点阅读