创建方式汇总,注册和不注册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的复用机制问题总结的更多相关文章

  1. cell 的复用机制

    一个问题引发的血案,以下是本侦探的探案过程的一部分:以下全部都是转载自别人的博客:http://blog.sina.com.cn/s/blog_9c3c519b01016aqu.html 转自:htt ...

  2. cell的复用机制

    以下全部都是转载自别人的博客:http://blog.sina.com.cn/s/blog_9c3c519b01016aqu.html 转自:http://www.2cto.com/kf/201207 ...

  3. tableView中cell的复用机制

    TableView的重用机制,为了做到显示和数据分离,IOS tableView的实现并且不是为每个数据项创建一个tableCell.而是只创建屏幕可显示最大个数的cell,然后重复使用这些cell, ...

  4. Tableview 优化Cell的复用机制01

    #import "ViewController.h" @interface ViewController ()<UITableViewDataSource> @end ...

  5. QF——UITableViewCell性能优化(视图复用机制)

    这几篇博客总结的不错: 点击进入 点击进入 总结起来方案一般有以下几种: 1.不使用透明视图: 2.减少视图的个数: 3.cell复用机制:(重点) 4.图片缓存: 5.网络请求使用非主线程. 6.预 ...

  6. Android学习笔记之ListView复用机制

    PS:满打满算,差不多三个月没写博客了...前一阵忙的不可开交...总算是可以抽出时间研究研究其他事情了... 学习内容: 1.ListView的复用机制 2.ViewHolder的概念 1.List ...

  7. iOS--cell的重用机制

    对于像我们这样的初学者来说,cell重用机制是很难理解的内容,所以我们不一定非得理解,会用就行. cell的重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell的个数+1,当ce ...

  8. 再谈select, iocp, epoll,kqueue及各种I/O复用机制

    原文:http://blog.csdn.net/shallwake/article/details/5265287 首先,介绍几种常见的I/O模型及其区别,如下: blocking I/O nonbl ...

  9. I/O复用机制概述

    导读 /O多路复用技术通过把多个I/O的阻塞复用到同一个select的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求.与传统的多线程/多进程模型比,I/O多路复用的最大优势是系统开销小 ...

随机推荐

  1. Python文件操作生成csv及其他存储类型

    通常Pandas用习惯后,比较喜欢用.to_csv的操作直接来转成csv文件,但如果是对于列表,则可以使用文件操作生成写入csv文件: #打开文件fid0=open('baseline.csv','w ...

  2. Eclipse ALT+/ 代码没有提示功能

    第一种配置如下: 第二: 第三: 以上三种方式是关于eclipse代码提示

  3. mysql常用内置函数-查询语句中不能使用strtotime()函数!

    来自:http://yushine.iteye.com/blog/775407 FROM_UNIXTIME把 unix时间戳转换为标准时间 unix_timestamp把标准时间转换为 unix时间戳 ...

  4. error LNK2001: 无法解析的外部符号 __imp__Shell_NotifyIconA@8

    编译链接报错 error LNK2001: 无法解析的外部符号 __imp__Shell_NotifyIconA@8 解决方案: 在代码中添加链接库Shell32.lib #pragma commen ...

  5. PHP面试 AJAX基础内容

    AJAX基础内容 Ajax的基本工作原理 Ajax基础概念:通过在后台与服务器进行少量数据交换,Ajax可以使用网页实现异步更新 Ajax工作原理:XMLHttpRequest是Ajax的基础     ...

  6. __user表示是一个user mode的pointer,所以kernel不可能直接使用。

    __user表示是一个用户空间的指针,所以kernel不可能直接使用. #ifdef __CHECKER__# define __user __attribute__((noderef, addres ...

  7. call和apply实现的继承

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. nginx匹配以XXX结尾的

    匹配以do结尾的所有文件:如http://192.168.126.168:8080/delivery/transportPlanData.do?startRelease=2019-07-06& ...

  9. python--知识小结和集合

    知识小结 一. = 表示赋值 ==表示比较值的大小 is 比较内存地址 二. 数字小数据池:在-5~256之内,id在电脑里是一样的 字符串id一样要求: ①:不能有特殊字符 ②:s(一个单字符)*2 ...

  10. RestTemplate java.net.UnknownHostException

    背景:公司内部系统的架构升级准备用微服务一套:记录遇到的坑. 错误信息: Servlet.service() for servlet [dispatcherServlet] in context wi ...