iOS学习——UITableViewCell两种重用方法的区别
今天在开发过程中用到了UITableView,在对cell进行设置的时候,我发现对UITableViewCell的重用设置的方法有如下两种,刚开始我也不太清楚这两种之间有什么区别。直到我在使用方法二进行重用的时候,具体实现代码如下,其中CJMeetingReplyBasicCell是我自定义的UITableViewCell类型,但是在运行的时候每次都在调用 CJMeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath]; 时崩溃,通过查找各种原因,确定不是自己代码的问题之后,开始了解这两种重用方法的区别。那么,这两种重用UITableViewCell的方法之间到底有什么区别呢?
//方法一
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//方法二
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
if (!cell) {
cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"];
}
return cell;
}
一 官网文档解释
首先我们看一下在iOS源码的UITableView.h中对两者的解释如下,我们可以看到方法二是在iOS 6.0中开始推出的新方法,在对方法二的解释中,我们注意标红的部分的意思是假设我们已经注册了标识符,这里我们猜测可能是我们需要对标识符进行注册。
//UITableView.h
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (__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
接下来我们找到官方文档,https://developer.apple.com/documentation/uikit/uitableview/1614878-dequeuereusablecellwithidentifie?language=objc,官方文档对方法二的解释有两点需要注意,第一个是返回值的地方如下图,这里说道该方法总是返回一个有效的UITableViewCell,这是与方法一不同的地方之一。
第二个需要注意的地方是,在该页面下面有一个Important的提示如下图,该提示就说明了方法二的正确使用方法。这里解释说要先进行注册我们自定义或者通过nib的类和标识符,然后再使用方法二进行重用。所以现在我们崩溃的原因就已经明确了,问题就出在没有进行先注册我们自定义的类和标识符。
二 常规使用方法
对于这两种方法的常规使用方法,下面进行总结一下。
首先,对于方法一,使用方法很简单,无需进行其他的定义和注册,代码如下。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell"];
if (!cell) {
cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"];
}
return cell;
}
接下来,我们简单使用方法二进行重用,具体步骤代码如下。
//首先,我们再tableview进行配置的时候需要注册我们已定义的cell类和重用标识符
self.tableView.backgroundColor = xxxx;
[self.tableView registerClass:[MeetingReplyBasicCell class] forCellReuseIdentifier:@"BasicCell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
return cell;
}
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- 结构型设计模式:适配器模式和门面模式
- 结构型设计模式:代理模式
- kubernete中的原子调度单位:pod
- mybatis-generator在命令行及IEAD中的使用
- mybatis-generator在命令行及IDEA中的使用
- 70-STM32+ESP8266+AIR202基本控制篇-移植使用-移植单片机MQTT底层包到自己的工程项目
- springboot研究:springboot自带监控actuator
- springboot研究:springboot使用swagger自动构建api
- numpy/pandas瞎搞系列(一):OLS,WLS的numpy实现
- redis实战第三篇 redis sentinel安装和部署
- 后浪,谈谈你对jvm性能调优的理解
- Homer预测共表达基因的motif
- redis实战第一篇 安装和使用
- lombok在java项目中的使用
- MySQL 的全文索引.