tableview的reloadData应注意
2012-08-06 13:32
357 查看
tableview的reloadData应注意
分类: iphone开发2011-11-2515:54 1054人阅读 评论(0) 收藏 举报
UITableView reloadData的正确方法。
相信很多人会遇到这种情况,当tableView正在滚动的时候,如果reloadData,偶尔发生App crash的情况。 这种情况有时候有,有时候没有,已经难倒了很多人。直至今天,我在stackoverflow上面,仍没有发现真正有说到其本质的帖子。我的处女贴,选择这个问题来阐述一下我的观点。
小弟我英语很好,一般都是用英语记笔记,当然,我知道,论坛愤青很多,如果只贴英文出来,肯定找骂。 故简单翻译一下,以显示我的诚意。 原英文笔记附在后面。 请大家不要挑英语语法错误了,笔记就是笔记,不是出书。
第一句话,阐述问题的本质:在tableView的dataSource被改变 和 tableView的reloadData被调用之间有个时间差,而正是在这个期间,tableView的delegate方法被调用,如果新的dataSource的count小于原来的dataSource count,crash就很有可能发生了。
下面的笔记提供了两种解决方案,和记录了一个典型的错误,即 在background thread中修改了datasource,虽然调用 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nilwaitUntilDone:NO];
记住正确的原则: Always change the dataSource and(注意这个and) reloadData in the mainThread. What's more, reloadData should be called immediately after the dataSource change.
If dataSource is changed but tableView's reloadData method is not called immediately, the tableView may crash if it's in scrolling.
Crash Reason: There is still a time gap between the dataSource change and reloadData. If the table is scrolling during the time gap, the app may Crash!!!!
WRONG WAY:
Following codes is WRONG: even the reloadData is called in main thread, there is still a time gap between the dataSource change and reloadData. If the table is scrolling during the time gap, the app may Crash!!!!
wrong codes samples:
-(void) changeDatasource_backgroundThread
{
@autoreleasepool{
[self.dataSourceArray removeAllObjects];
[self.tableViewperformSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
RIGHT WAY:
Principle: Always change dataSource in MAIN thread and call the reloadData immediately after it.
Option 1: If the operation to change the dataSource should be executed in background, the operation can create a temp dataSource array and pass it to main thread with notification, the main thread observes the notification, assign
the tmpDataSource to dataSource and reload the tableView by reloadData.
Option 2: In the background, call the GDC dispatch_async to send the two methods to main thread together.
dispatch_async(dispatch_get_main_queue(), ^{
self.dataSourceArray= a new Array.
[self.tableView reloadData];
});
相关文章推荐
- tableview的reloadData应注意
- tableview的reloadData应注意
- tableview在编辑模式下reloaddata后 原来选中的cell不走diddeselected方法
- tableView 数据源修改后,reloaddata数据没有更新
- 源码推荐(6.26):三级联想菜单(也可以当二级),tableView reloadData动画(swift)
- tableView.reloadData()无效
- 如何检测tableview reloaddata结束【ios】
- iOS tableView reloadData 抖动问题
- tableview调用reloadData()之后界面不刷新显示
- 郑州iOS點 - 关于tableView 和 collectionView 在ReloadData时候闪烁的问题
- tableView reloadData
- reloaddata 是没有对 tableview的 headerview进行 刷新的,
- 关于CCTableView中reloadData()操作,删除第一个/最后一个cell后tableview位置未刷新的问题
- iOS用SegmentController来切换tableView的时候 reloadData会造成数据重复创建 解决办法
- 在textFieldShouldEndEditing 方法中调用[self.tableView reloadData] 失效问题
- tableView之reloadData方法
- Cocos2d-x学习之---关于CCTableView中reloadData()操作,删除第一个/最后一个cell后tableview位置未刷新的问题
- iOS开发UITableView篇:[self.tableView reloadData]执行时间过长问题解决历程
- iOS tableView reloadData不生效或出现问题
- [tableView reloadData]方法添加动画的方法