Cell的复用机制问题总结
创建方式汇总,注册和不注册Cell注册的两种方式
1.tableView registerNib:(nullable UINib *) forCellReuseIdentifier:(nonnull NSString *)
2.tableView registerClass:(nullable Class) forCellReuseIdentifier:(nonnull NSString *)
Cell注册的形式:
(1)系统cell
1.注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
2.不注册
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
(2)自定义cell
1.注册
[self.tableView registerClass:[xxxxCell class] forCellReuseIdentifier:@"cell"];
xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
2.不注册
xxxxCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[xxxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
(3)自定义cellXib注册
1.注册(可以复用)
[tableView registerNib:[UINib nibWithNibName:@"xxxxViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
2.不注册(不会复用,每一次都是重新创建)
xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@“xxxxCell" owner:self options:nil]lastObject];
}
复用机制不多做赘述,只讲解一下注册的复用机制
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: identifier] ;
这个方法的作用是,当我们从重用队列中取cell的时候,如果没有,系统会帮我们创建我们给定类型的cell,如果有,则直接重用. 这种方式cell的样式为系统默认样式.在设置cell的方法中只需要:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 重用队列中取单元格 由于上面已经注册过单元格,系统会帮我们做判断,不用再次手动判断单元格是否存在
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier forIndexPath:indexPath] ;
return cell ;
}
注册和不注册的区别:
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
// Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
(返回给代理一个已经分配的cell,代替一个新的cell,如果没有已分配的cell,则返回nil,使用这个方法就不需要注册了)
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
如果cell的identifier是注册过的,那么这个新列出的方法保证返回一个cell (有分配的就返回已分配的cell,没有返回新的cell)并适当调整大小,可省略cell空值判断步骤,用这个方法cell必须注册,不是自定义的cell,UITableViewCell也要注册
Cell的复用机制问题总结的更多相关文章
- cell 的复用机制
一个问题引发的血案,以下是本侦探的探案过程的一部分:以下全部都是转载自别人的博客:http://blog.sina.com.cn/s/blog_9c3c519b01016aqu.html 转自:htt ...
- cell的复用机制
以下全部都是转载自别人的博客:http://blog.sina.com.cn/s/blog_9c3c519b01016aqu.html 转自:http://www.2cto.com/kf/201207 ...
- tableView中cell的复用机制
TableView的重用机制,为了做到显示和数据分离,IOS tableView的实现并且不是为每个数据项创建一个tableCell.而是只创建屏幕可显示最大个数的cell,然后重复使用这些cell, ...
- Tableview 优化Cell的复用机制01
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource> @end ...
- QF——UITableViewCell性能优化(视图复用机制)
这几篇博客总结的不错: 点击进入 点击进入 总结起来方案一般有以下几种: 1.不使用透明视图: 2.减少视图的个数: 3.cell复用机制:(重点) 4.图片缓存: 5.网络请求使用非主线程. 6.预 ...
- Android学习笔记之ListView复用机制
PS:满打满算,差不多三个月没写博客了...前一阵忙的不可开交...总算是可以抽出时间研究研究其他事情了... 学习内容: 1.ListView的复用机制 2.ViewHolder的概念 1.List ...
- iOS--cell的重用机制
对于像我们这样的初学者来说,cell重用机制是很难理解的内容,所以我们不一定非得理解,会用就行. cell的重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell的个数+1,当ce ...
- 再谈select, iocp, epoll,kqueue及各种I/O复用机制
原文:http://blog.csdn.net/shallwake/article/details/5265287 首先,介绍几种常见的I/O模型及其区别,如下: blocking I/O nonbl ...
- I/O复用机制概述
导读 /O多路复用技术通过把多个I/O的阻塞复用到同一个select的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求.与传统的多线程/多进程模型比,I/O多路复用的最大优势是系统开销小 ...
随机推荐
- Linux下安装Tomcat服务器
Linux下安装Tomcat服务器 一.总结 一句话总结: linux多用才能熟 1.阿里云上面我们买的服务器,怎么让它可以访问特定的端口? 就是给服务器的安全组添加规则:实例-->更多--&g ...
- 【消息中间件】kafka
一.kafka整体架构 kafka是一个发布订阅模式的消息队列,生产者和消费者是多对多的关系,将发送者与接收者真正解耦: 生产者将消息发送到broker: 消费者采用拉(pull)模式订阅并消费消息: ...
- 2019牛客多校第⑨场E All men are brothers(并查集+组合数学)
原题:https://ac.nowcoder.com/acm/contest/889/E 思路: 做并查集,维护每个集合大小,初始化操作前的总方案数,每次合并两个集合时减少的数量=合并的两个集合大小相 ...
- list列表操作(创建、增加、删除、取值)
list ####(一)列表的创建[].追加(append,extend,insert).删除(remove.del.poop).修改 ##创建一个空列表.一个字符串列表.一个数字列表 lis0 = ...
- zoom:1总结
zoom:1确实帮我们解决了不少ie下的bug,但是它的来龙去脉,又有多少人知道呢? 所以我老生常谈,说一下它的来龙去脉. Zoom属性是IE浏览器的专有属性, 它可以设置或检索对象的缩放比例.先来一 ...
- jmeter 不同线程组之间传递变量3
jemter编写脚本要点: 1.切记:BeanShell PostProcessor写在关联函数 Regular Expression Extractor的后面 2.header HTTP Head ...
- 关于char(M)和varchar(N)的区别
1.int(10)的10表示显示的数据的长度,不是存储数据的大小:chart(10)和varchar(10)的10表示存储数据的大小,即表示存储多少个字符. int(10) 10位的数据长度 9999 ...
- Spring Boot 2.1.6 发布了!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 最新消息: Spring Boot 2.1.6 昨天正式发布了,日常更新一些依赖和修复一些 BUG,没什么硬菜! 重点来了, ...
- Mysq sql语句教程
mysql管理命令 show databases; 显示服务器上当前所有的数据库 use 数据库名称; 进入指定的数据库 show tables; 显示当前数据库中所有的数据表 d ...
- Android 导致OOM的常见原因
OOM主要有两种原因导致: 1. 加载大图片: 2. 内存泄漏: 一.加载大图片 在Android应用中加载Bitmap的操作是需要特别小心处理的,因为Bitmap会消耗很多内存.比如,Galaxy ...