引言

对于一个UITableView而言,可能需要显示成百上千个Cell,如果每个cell都单独创建的话,会消耗很大的内存。为了避免这种情况,重用机制就诞生了。

假设某个UITableView有100个数据需要显示,即需要100个Cell,然而屏幕中最多只能一次性显示10个Cell,那么有一个办法可以不用创建100cell,而只需要创建11(10+1)个。

理解

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}

我们来理解这段代码:

static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

这两句代码的作用是:根据标识符identifier从重用队列中取出一个cell(先不用管重用队列是什么),由于一开始重用队列是空的,所以取出的cell也是空的,if(!cell)条件成立,就会去执行{}内的代码

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

创建UITableViewCellStyleDefault类型的cell,并将其标识为identifier(@”cell”);这样一个cell就创建完成了。当需要第2个cell的时候,同样也是先从重用队列里去取,发现还是没有,那么继续创建。可想而知,刚进到这个页面的时候需要创建10个cell,且他们的标识符都是identifier。

当我们下拉tableView,我们知道会出现第11个cell。这个时候还是先从重用队列里去取,发现没有,继续创建第11个cell并返回,这个时候,当第11个cell完全显示出来,第1个cell也刚好完全消失,它去哪了呢?第1个cell(标记为cell1)被放进重用队列了”。

再继续下拉,就要出现第12个cell了。那么还是继续创建吗?之前说过一共要创建11个cell,那么至此所有的cell都创建完毕了,那第12个cell是怎么来的呢?同样的,还是要先调用dequeueReusableCellWithIdentifier:方法,从重用队列中寻找cell,这个时候队列中有cell1,就会被取出来,这时候if(!cell)条件不成立,也就不会创建新的cell了,这个cell被返回作为第12个cell。可想而知,当第12个cell完全显示,第2个cell就完全消失进入重用队列了,再往下拉cell(2)就会作为第13个cell出现。就是这么神奇!

这就是重用机制,尽管需要100个cell,但事实上只创建了11个cell,这些cell重复利用,在需要的时候扮演不同的角色(只是换了件衣服,还是同一个人)。

identifier

可以看到在创建cell的时候伴随着一个identifier的绑定,这个identifier可以理解为这个cell标识,标识它属于哪个重用队列。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

再来看这句代码,从重用队列中取出一个cell,注意传入的参数identifier,如果把重用队列比作一个房间,那么identifier就好比这个房间的门牌号,标记着要从指定的房间去找人(也就是cell)。另外,入队的时候也会根据cell的identifier放到指定的重用队列中。

可想而知,因为上面那段代码所有的cell都用了相同的identifier,所以只会在一个重用队列中进进出出。假如把代码改成这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}

创建cell的时候对每个cell绑定不同的identifier,那么每个cell在入队的时候就会被放到不同的队列中,这样第一遍下拉100个cell都是后每次调用dequeueReusableCellWithIdentifier都无法在对对应重用队列中找到cell,因此要创建100个cell,这就增大了消耗。

注册cell

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];

可以在创建tableView的时候用这个方法注册cell,注册之后的作用是每次从重用列表中取cell 的时候,假如不存在,系统就会自动帮我们创建cell,并绑定标识符identifier。可想而知,注册之后就不需要再写这段代码了:

if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

解决列表重复混乱问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 100, 50)];
label.text = [NSString stringWithFormat:@"测试%d%d%d",(int)indexPath.row,(int)indexPath.row,(int)indexPath.row];
[cell addSubview:label];
return cell;
}

我们对每个cell添加了一个子视图label,运行后重复下拉上拉,会发现出现了列表混乱的现象!

如果你理解了重用的本质,就不难知道其中的原因。简单的说,因为每次新出现的cell都是用过的,再对它添加label就是在原来已经有label的基础上又多了一个label,这就显示混乱了。解决方法如下:

方法一

将方法

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

用下面方法替换

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

这种方法放弃了重用机制,每次根据indexPath获取对用的cell返回。

方法二

对每个cell设置不同的identifier,这种方式在前面介绍过,同样能解决列表重用的问题,虽然保留了重用机制,但是还是需要创建100个cell,性价比不高。

方法三

删除重用cell的子视图,即每次将cell从重用列表中取出重新使用的时候,先将其原有的所有子视图移除,这样就不会出现混乱了,这是方法保留了重用机制,且创建的cell数量最小化,性能比较高。代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
} //移除所有子视图
[cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *subView = (UIView *)obj;
[subView removeFromSuperview];
}];
//添加新视图
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 100, 50)];
label.text = [NSString stringWithFormat:@"测试%d%d%d",(int)indexPath.row,(int)indexPath.row,(int)indexPath.row];
[cell addSubview:label]; return cell;
}

至此,UiTableView的重用机制介绍完毕。

 

IOS开发—UITableView重用机制的了解的更多相关文章

  1. 操蛋的UITableView重用机制

    1,背景 你可能会遇见一下情况: 使用UITableView加载数据,比如你的每一个cell上面有一个UITextField,当你在第一个cell的UITextField中写下了内容,开始下滑UITa ...

  2. IOS之TableViewCell重用机制解决上下刷新重复显示

    首先我是一个经验浅薄的iOS开发人员,这个问题想必许多初学者经常遇到这些问题,在面试中也会经常问到.现在我们一一解决. 首先我们要知道TableViewCell重用机制的原理是什么,我们抽象的理解为古 ...

  3. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  4. UI之UItableView重用机制的性能问题

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  5. ios之TableViewCell重用机制避免反复显示问题

    常规配置例如以下 当超过tableView显示的范围的时候 后面显示的内容将会和前面反复 // 这样配置的话超过页面显示的内容会反复出现 - (UITableViewCell *)tableView: ...

  6. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  7. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  8. IOS之TableViewCell重用机制避免重复显示问题

    常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复. 1 // 这样配置的话超过页面显示的内容会重复出现 2 - (UITableViewCell *)tableVi ...

  9. iOS开发事件分发机制—响应链—手势影响

    1.提纲 什么是iOS的事件分发机制 ? 一个事件UIEvent又是如何响应的? 手势对于响应链有何影响? 2.事件分发机制 2.1.来源 以直接触摸事件为例: 当用户一个手指触摸屏幕是会生成一个UI ...

随机推荐

  1. tomcat配置https方法

    利用tomcat服务器配置https双向认证 步骤 1.为服务器生成证书进入控制台,切换到%JAVA_HOME%/bin目录,具体操作略. 使用keytool为Tomcat生成证书,假定目标机器的域名 ...

  2. c3p0私有属性checkoutTimeout设置成1000引发的调试错误:

    checkoutTimeout设置成1000引发的调试错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.ap ...

  3. 关于ajax跨域问题

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

  4. IO流数据读写总结

    1.用java自带的IO读写方法 官方API网站:http://docs.oracle.com/javase/7/docs/api/ 2.Apache的Commons-io-2.4.jar中的方法,参 ...

  5. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  6. C++设计模式-singleton单例模式_new

      class nocopyable { protected: nocopyable(){}; virtual ~nocopyable(){}; nocopyable(const nocopyable ...

  7. read/sysread区别

    use warnings; use strict; my $readbuff; my $sysreadbuff; ); print "read get:$readbuff\n"; ...

  8. Mysql存储过程和函数

    Mysql存储过程和函数 基本概念: 创建存储过程和函数是指将经常使用的一组SQL语句的组合在一起,并将这些SQL语句当作一个整体存储在MySQL服务器中.例如,银行经常需要计算用户的利息.不同类别的 ...

  9. 共享AFHTTPSessionManager 单例好处浅析

      很多时候,AFNetworking都是目前iOS开发者网络库中的不二选择.Github上2W+的star数足见其流行程度.而从iOS7.0开始,苹果推出了新的网络库继承者NSURLSession后 ...

  10. sql server 2012提示评估期已过的解决办法 附序列号

    sql server 2012提示评估期已过的解决方法: 第一步:进入SQL2012配置工具中的安装中心. 第二步:再进入左侧维护选项界面,然后选择选择版本升级. 第三步:进入输入产品密钥界面,输入相 ...