博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITableView的cell重用优化
阅读量:4691 次
发布时间:2019-06-09

本文共 2612 字,大约阅读时间需要 8 分钟。

三种情况,四种方法:

情况一:加载xib中描述的cell

情况二:加载纯代码自定义的cell

情况三:加载storyBoard中的tableView内的cell

 

针对于情况一:

// 导入自定义cell的.h文件,在viewDidLoad方法中注册xib中描述的cell,因为只需要注册一次,所以选择在viewDidLoad方法中注册#import "WSUserCell.h"- (void)viewDidLoad {[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WSUserCell class]) bundle:nil] forCellReuseIdentifier:WSUserCellId];}

针对于情况二:

// 方法一:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *ID = @"carID";    UITableViewCell *carCell = [tableView dequeueReusableCellWithIdentifier:ID];    if (carCell == nil) {        carCell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];    }    WSCarGroups *carGroup = self.arrayModel[indexPath.section];    WSCars *car = carGroup.cars[indexPath.row];        carCell.textLabel.text = car.carName;    carCell.imageView.image = [UIImage imageNamed:car.carIcon];        return carCell;}
// 方法二:- (void)viewDidLoad {// 注册 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *ID = @"carID";    UITableViewCell *carCell = [tableView dequeueReusableCellWithIdentifier:ID]; // 因为已经注册过这个cell类,所以就不用写这句代码 //    if (carCell == nil) {//        carCell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];//    }    WSCarGroups *carGroup = self.arrayModel[indexPath.section];    WSCars *car = carGroup.cars[indexPath.row];        carCell.textLabel.text = car.carName;    carCell.imageView.image = [UIImage imageNamed:car.carIcon];    return carCell;}

 针对于情况三:

即不需要注册,也不需要在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中判断cell是否为空,系统会自动加载storyBoard中指定reuseIdentifier的cell

- (void)viewDidLoad {    [super viewDidLoad];        self.tableView.rowHeight = 70;    // 千万不要注册,因为注册的优先级大于storyBoard,一旦注册就不会加载storyBoard中的cell//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tg"];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row % 2 == 0) {        static NSString *ID = @"tg";        XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];                cell.tg = self.tgs[indexPath.row];                return cell;    } else {        return [tableView dequeueReusableCellWithIdentifier:@"test"];    }}

如下图,为storyBoard中给cell绑定的reuseIdentifier:

 

转载于:https://www.cnblogs.com/wsnb/p/4789808.html

你可能感兴趣的文章
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
lock_sga引起的ksvcreate :process(m000) creation failed
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
11.5 内部类
查看>>