三种情况,四种方法:

情况一:加载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 = ;
// 千万不要注册,因为注册的优先级大于storyBoard,一旦注册就不会加载storyBoard中的cell
// [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tg"];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % == ) {
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:

UITableView的cell重用优化的更多相关文章

  1. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  2. 解决UITableView中Cell重用机制导致内容出错的方法总结

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  3. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  4. iOS开发之UITableView及cell重用

    1.UITanleview有的两种风格 一种是Plain,一种是Grouped,可以从这里设置风格: 他们样式分别如下: Plain: Grouped: 2.tableView展示数据的过程: (1) ...

  5. iOS解决UITableView中Cell重用带来的问题

    tableView的常规配置,当超出一屏的cell就会标上可重用的标识出列到可重用缓存池中,后面再根据可重用标识来到的可重的cell就会和前面显示同样内容. - (UITableViewCell *) ...

  6. UITableView中的cell的优化

    1.cell的重用 所谓的cell的重用就是,视图加载的时候只会创建当前视图中的cell,或者比当前视图多一点的cell, 当视图滚动的时候,滚出屏幕的cell会放进缓存中,滚进屏幕的cell会根据I ...

  7. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  8. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  9. 解决Cell重用内容混乱的几种简单方法,有些方法会增加内存

    重用实现分析 查看UITableView头文件,会找到NSMutableArray*  visiableCells,和NSMutableDictnery* reusableTableCells两个结构 ...

随机推荐

  1. javascript时间的一些问题

    1.求当前时间. var curDate = new Date(); 2.求当前时间的前一天 var preDate = new Date(curDate.getTime()-24*60*60*100 ...

  2. PHP & Delphi 語法

    明 C(区分大小写) Delphi(不区分大小写) PHP(区分大小写) 整型变量的定义 1 2 3 4 5 6 7 char a = 'a';         /* 8位有符号*/ int a=10 ...

  3. DEDECMS之四 栏目调用

    一.内容页调用 {dede:type} <a href=" [field:typelink /] "> [field:typename/] </a> {/d ...

  4. servlet 中文乱码问题

    两步骤搞定: 1,修改tomcat的server.xml <Connector port="8080" protocol="HTTP/1.1" conne ...

  5. C#TCP通讯框架

    开源的C#TCP通讯框架 原来收费的TCP通讯框架开源了,这是一款国外的开源TCP通信框架,使用了一段时间,感觉不错,介绍给大家 框架名称是networkcomms 作者开发了5年多,目前已经停止开发 ...

  6. 图片加载框架Picasso解析

    picasso是Square公司开源的一个Android图形缓存库 主要有以下一些特性: 在adapter中回收和取消当前的下载: 使用最少的内存完成复杂的图形转换操作: 自动的内存和硬盘缓存: 图形 ...

  7. c++基础 explicit

    c++的构造函数也定义了一个隐式转换 explicit只对构造函数起作用,用来抑制隐式转换 看一个小例子 新建一个头文件 #ifndef CMYSTRING_H #define CMYSTRING_H ...

  8. 我的 GitHub 100 连击

    终于达成 gayhub 的第一个100连击了,感觉自己整个人颜色都不一样了,完全蜕变了. PS: GitHub 汉化插件 52cik/github-hans 感兴趣的赶紧 get 起来吧. 遇到瓶颈 ...

  9. [BZOJ 2819]NIM(dfs序维护树上xor值)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2819 分析: 树上的nim游戏,关键就是要判断树上的一条链的异或值是否为0 这个题目有 ...

  10. JS 问题集锦

    [1]js页面跳转 和 js打开新窗口方法 第一种: <script language="javascript" type="text/javascript&quo ...