您的位置:首页 > 产品设计 > UI/UE

iOS开发:UITableView的deleteRowsAtIndexPaths方法的使用

2016-07-21 09:56 543 查看
在做一个类似微博的无限长列表的时候,内存的消耗会比较大,常常会触发 didReceiveMemoryWarning 方法,所有有必要在didReceiveMemoryWarning做一些数据释放。

于是删除数据源后 执行[UITableView reloadData]方法,但是此方法会让tableView重新刷新一遍界面,视觉上会有一闪这种影响体验的感觉,于是尝试使用deleteRowsAtIndexPaths方法,使用这个方法容易出现闪退的情况,错误的log如下,原因基本都是因为数据源的删除与删除的cell不匹配导致的,需要仔细检查。

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (54) must be
equal to the number of rows contained in that section before the update (110), plus or minus the number of rows inserted or deleted from that section (0 inserted, 54 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in,
0 moved out).'

deleteRowsAtIndexPaths方法注意点:

1.在执行deleteRowsAtIndexPaths方法前,删除dataSource,并确保删除的dataSource与删除的cell严格匹配。

2.追求用户体验的,需要标记当前显示的cell,方便删除多余cell后再重新回到显示之前的位置。

另:deleteRowsAtIndexPaths和reloadData方法的成本哪个更高(据说是第一个更高效,第二个成本更高),没有做深入探究,有兴趣的朋友可以研究研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: