UITableView reloadData的正确方法
来源:互联网 发布:数据库查询结果转json 编辑:程序博客网 时间:2024/05/16 04:42
相信很多人会遇到这种情况,当tableView正在滚动的时候,如果reloadData,偶尔发生App crash的情况。 这种情况有时候有,有时候没有,已经难倒了很多人。直至今天,我在stackoverflow上面,仍没有发现真正有说到其本质的帖子。
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nilwaitUntilDone:NO];
记住正确的原则: Always change the dataSource and(注意这个and) reloadData in themainThread. 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, theoperation 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 thetableView by reloadData.
Option 2:In the background, call the GDC dispatch_async to send the two methods to mainthread together.
self.dataSourceArray= a new Array.
[self.tableView reloadData];
});
第一句话,阐述问题的本质:在tableView的dataSource被改变 和 tableView的reloadData被调用之间有个时间差,而正是在这个期间,tableView的delegate方法被调用,如果新的dataSource的count小于原来的dataSource count,crash就很有可能发生了。
下面的笔记提供了两种解决方案,和记录了一个典型的错误,即 在background thread中修改了datasource,虽然调用记住正确的原则:
If dataSource is changed but tableView's reloadData method is not called immediately, the tableView may crash if it's in scrolling.
Crash Reason:
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
[self.tableViewperformSelectorOnMainThr
}
RIGHT WAY:
Principle:
Option 1:
Option 2:
dispatch_async(dispatch_get_main_queue(), ^{
//如果self.dataSourceArray已改变,直接写成self.dataSourceArray = self.dataSourceArray;也OK
});
个人的第三个解决方案
在reloadData前,把dataSource和delegate设置为nil,reloadData前再设置回来
0 0
- UITableView reloadData的正确方法
- UITableView reloadData的正确方法。
- UITableView reloadData的正确方法
- UITableView reloadData的正确方法
- UITableView reloadData的正确方法
- UITableView reloadData的正确方法。请每一个开发者必看。
- 我的处女贴: UITableView reloadData的正确方法。请每一个开发者必看。《转》
- 判断UitableView reloadData 结束的方法
- UITableView的reloadData方法,不执行cellForRow方法,
- UITableView使用reloadData的几种动画方法
- iOS开发~为UITableView的reloadData方法添加动画
- UITableView reloadData的本质使用
- mac下在NSThread线程中reloadData的正确方法
- UITableView的reloadData方法,不执行cellForRow方法,但是执行了numberOfRows和heightForRow方法
- UITableView调用reloadData不刷新的Bug
- 调用uitableview的reloadData函数没效果
- 解决iOS UITableView reloadData 时闪屏的问题
- iOS UITableView reloadData刷新的动画
- 嵌入式Boa服务器 中用CCGI 编程出现的 弹出下载弹框的解决办法
- Runtime.getRuntime().addShutdownHook(thread)的作用
- 东京地震8.0 剧本5 真理的独白
- (8) cocos2d-x中相关的一些C++函数
- 激活Maven profile的几种方式
- UITableView reloadData的正确方法
- 【PAT】1086. Tree Traversals Again (25)
- Emacs 学习笔记
- 使用SqlDependency类及利用SQL2008的Service Broker来检测数据改动的实现
- intel media SDK ----摘自ww506772362的专栏
- JS 需要一个JavaScript 的API 希望有的朋友帮下忙,邮箱1360737271@qq.com
- Android LayoutInflater详解
- 软件测试常见的智力题
- 洪涛-2014:分化的国内手游市场