http://blog.csdn.net/ouyangtianhan/article/details/7835041

http://stackoverflow.com/questions/16071503/how-to-tell-when-uitableview-has-completed-reloaddata

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应注意的更多相关文章

  1. tableview调用reloadData()之后界面不刷新显示

    解决方法: 查看是否有指定tableView的delegate和datasource. self.tableView.delegate = self self.tableView.datasource ...

  2. iOS 解决TableView reloadData时cell中图片会闪的问题

    tableView调用reloaddata的时候发现有个小问题,每次刷新图片都会抖动闪烁一下,看着很难受,也影响体验.造成这个问题的主要原因是因为刷新时候切换图片导致.要解决这个问题也很好解决,使用S ...

  3. iOS深入学习(UITableView系列2:reloadData)

    接着前一篇的博客来深入学习UITableView, UITableView的数据源是NSMutableArray的对象_infoArray,现在数组的内容为{@"Zero",@&q ...

  4. collectionView/tableview刷新时关闭动画无效

    collectionView/tableview reloadSections/reloaddata时去掉动画无效时可以尝试使用 [UIView performWithoutAnimation:^{ ...

  5. [iOS基础控件 - 6.9.3] QQ好友列表Demo TableView

    A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组   code so ...

  6. 【iOS开发-59】LOL案例:单组tabView、alertView样式、实现监听,以及用reloadData数据刷新

    案例效果: (1)先在storyboard中拖拽出一个tableView,然后下面用代码. --tableView继承自scrollView.所以自然有滚动的特性 --最基本的还是数据转模型.以及对c ...

  7. tableView刷新中的问题

    在开始之前先上一张效果图 相信大家都看到了“店铺优惠”这一栏,在这里假设这一栏就是单独的一个cell,当无店铺优惠的时候不可点击在有店铺优惠的时候会弹出优惠列表,选中并返回时会刷新数据,所以弹出视图采 ...

  8. IOS设计模式之二(门面模式,装饰器模式)

    本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq) ...

  9. iOS端一次视频全屏需求的实现(转)

    对于一个带有视频播放功能的app产品来说,视频全屏是一个基本且重要的需求.虽然这个需求看起来很简单,但是在实现上,我们前后迭代了三套技术方案.这篇文章将介绍这三种实现方案中的利弊和坑点,以及实现过程中 ...

随机推荐

  1. Debian 安装Nvidia显卡驱动

    1.到nvidia官方网站下载自己显卡对应型号得驱动,如果不知道显卡型号,可以使用如下命令来查看 lspci | grep VGA 2.安装显卡驱动所必需得工具 apt-get install bui ...

  2. curl file_get_contents fsockopen

    三种处理的方式:     curl     file_get_contents     fsockopen fsockopen 是比较底层的调用,属于网络系统的socket调用,而curl经过的包装支 ...

  3. python 压缩 解压缩 文件

    1. 用zipfile模块打包文件或是目录.解压zip文件 http://wangwei007.blog.51cto.com/68019/1045577 #!/usr/bin/env python # ...

  4. 8633 回文划分(dp)

    8633 回文划分 该题有题解 时间限制:1000MS  内存限制:1000K提交次数:169 通过次数:63 题型: 编程题   语言: G++;GCC Description 我们说一个字符串是回 ...

  5. 为什么Hbase能实现快速的查询

    你的快速是指什么? 是根据亿级的记录中快速查询,还是说以实时的方式查询数据. A:如果快速查询(从磁盘读数据),hbase是根据rowkey查询的,只要能快速的定位rowkey,  就能实现快速的查询 ...

  6. UVA - 11400 Lighting System Design (区间DP)

    这个问题有两个点需要注意: 1. 对于一种灯泡,要么全换,要么全不换. 证明: 设一种灯泡单价为p1,电池价格为k1,共需要L个,若把L1个灯泡换成单价为p2,电池为k2的灯泡,产生的总花费为p1*L ...

  7. Illegal resource reference: @*android resources are private and not always present

    0:前言 在android开发中,当使用别人的代码的时候,在style.xml中有此种错误 1:解决方案 删除*星号

  8. 仿照微信的界面,即ViewPager+Fragment的结合使用

    主布局文件: android:drawableTop="@drawable/weixin_bg"用的是状态选择器,所以要写4个状态选择器,图片的 <RelativeLayou ...

  9. js 常用正则表达式表单验证代码

    正则表达式使用详解 简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.其作用如下:测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一 ...

  10. listview设置条目点击的时候不变色(让状态选择器不起作用)

    未设置前的效果如下图: 很明显,“酷狗音乐”那个条目被点击的时候,条目背景变为蓝色,怎么去掉这个颜色呢? java代码可以这么写: listView.setSelector(new ColorDraw ...