iOS进阶iOS Developer

iOS回顾笔记(09) -- Cell的添加、删除、更新、批量

2017-03-22  本文已影响224人  xiaoyouPrince

项目中经常有对UITableViewCell做各种操作的需求:

Snip20170322_4.png

下面就分别讲解一下工作中对Cell的各种操作

刷新数据方法

[self.tableView reloadData];
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];

cell批量操作

批量操作cell使用的方法和上面是一样的。不同的是批量操作的cell是待选中,然后统一删除。

思路:

数据刷新的原则

小结

修改cell,主要是从数据源中修改,因为tableview的数据都是从数据源中请求到的。

如果没有修改数据源,直接修改UI效果,当tableview滚动的时候tableview重新加载数据源又会覆盖原来的数据,导致cell的刷新失败。

上一篇 下一篇

猜你喜欢

热点阅读