iOS亲测UITableView重用机制,用事实说话。
来源:互联网 发布:大数据认证 有用吗 编辑:程序博客网 时间:2024/05/22 01:52
1、重用机制Cell是如何新建的?
很多人以为Cell在新建的时候只新建一个,其他的都重复使用那一个,但事实上并不是这样的,新建多少个Cell,并加入重用队列是取决于初始化时屏幕中能容纳多少个Cell,(5S模拟器测试)经测试Cell高44时,新建14个Cell。Cell高120时,count:6,新建6个Cell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static int count = 0; static NSString *celleIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celleIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleGray; count ++; } if (indexPath.row < 10) { cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; NSLog(@"count:%d,cellheight:%f",count,cell.frame.size.height); return cell;}
2、Cell重用时是如何取出来的?
在使用120高度的Cell时,重用队列中将只有6个存储的Cell,在下方代码中到第6个Cell,即textLabel == 6时 已用开始使用重用队列中的Cell,由打印的数据可知,init textLabel为0,这使用的是第一个存入的队列Cell,textLabel == 7时, init textLabel为1,使用的是存入的第二个Cell,一直到使用第六个完成一轮后再重新使用第一个存入的Cell,由此可以推断,Cell重用时是秉承先进先出的原则,循环使用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static int count = 0; static NSString *celleIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celleIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleGray; count ++; } NSLog(@"init textLabel:%@",cell.textLabel.text); if (indexPath.row < 10) { cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; } NSLog(@"count:%d,textLabel:%@",count,cell.textLabel.text); return cell;}
3、reuseIdentifier的作用?
在加载第11个Cell的时候开始,Cell的celleIdentifier换成了@"cell2",并且将第11个Cell的textLabel内容换乘@"this is 20",根据截图可知,由于更换了celleIdentifier,在第11个Cell加载时是没有init textLabel的,并且count开始自增直至12,说明使用新的celleIdentifier使tableview重新建了一个队列并且放入了6个新的Cell,并且在6个一轮之后重用了,新队列里的Cell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static int count = 0; static NSString *celleIdentifier = @"Cell"; if (indexPath.row > 10) { celleIdentifier = @"Cell2"; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celleIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleGray; count ++; } NSLog(@"init textLabel:%@",cell.textLabel.text); if (indexPath.row < 10) { cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; }else if (indexPath.row == 11){ cell.textLabel.text = @"this is 20"; } NSLog(@"count:%d,textLabel:%@",count,cell.textLabel.text); return cell;}
4、为什么使用重用机制后我的数据乱掉了?
在解释第三点时,在第11个Cell之后textLabel一直显示的是null,这是因为我没有在重用后,对这个Cell进行操作,一般在使用重用后,都需要对Cell的数据进行处理。如果没有处理的话,一种情况就像上面截图一样,由于重用队列里面的Cell的textLabel就是没有值的,取出来之后还是不进行操作,即为null。还有一种情况是打印出this is 20的这种情况,因为重用队列里面的Celld的textLabel里面有值,取出重用后不进行操作的话就会使用之前队列里Cell的textLabel这个值,所以后面有好几个重用Cell2队列中第一个存入的Cell时,打印出来的数据也为this is 20。
总结:不同的reuseIdentifier代表了不同的队列,重用时按队列顺序先进先出,一轮之后再重新从第一个开始重用。
- iOS亲测UITableView重用机制,用事实说话。
- 用事实说话
- iOS UItableview重用机制
- iOS UITableView的重用机制
- 用事实说话,用行动回报大家的支持!
- 用事实说话,我们的数据库应选择RAID几?
- 用事实说话:相比Google和苹果,微软到底有多牛?
- 用事实说话,还原一个真实的传智播客
- IOS开发之----UITableView重用机制
- ios UITableView中Cell的重用机制
- iOS—UITableView Cell重用机制
- IOS开发之----UITableView重用机制
- iOS之UITableView Cell重用机制
- ios UITableView中Cell的重用机制
- IOS之UITableView优化-可重用机制
- UItableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- javascript性能优化
- Effective Java——对象通用方法
- 实用SQL语句大全
- 2015-2-5冬令营日志
- QT5 +VS2010 SP1 解决中文乱码问题
- iOS亲测UITableView重用机制,用事实说话。
- iphone崩溃日志分析工具symbolicatecrash用法
- 房地产融资、投资,物业投资管理, 大骏期待与您合作
- poj解题报告——1129
- 编译Flume-NG遇到Too many unapproved licenses: 1
- JSP九大内置对象及四个作用域
- erlang- nifs 2
- linux 下的定时器。
- Win32API