UITableView中有两种重用Cell的方法:

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

在iOS 6中dequeueReusableCellWithIdentifier:被dequeueReusableCellWithIdentifier:forIndexPath:所取代。如此一来,在表格视图中创建并添加UITableViewCell对象会变得更为精简而流畅。而且使用dequeueReusableCellWithIdentifier:forIndexPath:一定会返回cell,系统在默认没有cell可复用的时候会自动创建一个新的cell出来。

使用dequeueReusableCellWithIdentifier:forIndexPath:的话,必须和下面的两个配套方法配合起来使用:

// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
// Instances returned from the new dequeue method will also be properly sized when they are returned.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

1、如果是用NIB自定义了一个Cell,那么就调用registerNib:forCellReuseIdentifier:

2、如果是用代码自定义了一个Cell,那么就调用registerClass:forCellReuseIdentifier:

以上这两个方法可以在创建UITableView的时候进行调用。

这样在tableView:cellForRowAtIndexPath:方法中就可以省掉下面这些代码:

static NSString *CellIdentifier = @"Cell";  

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)  {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

取而代之的是下面这句代码:

一、使用NIB

1、xib中指定cell的Class为自定义cell的类型(不是设置File's Owner的Class)

2、调用registerNib:forCellReuseIdentifier:向数据源注册cell

[_tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];   

3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的nib文件创建cell并返回如果使用dequeueReusableCellWithIdentifier:需要判断返回的是否为空

4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的awakeFromNib方法

二、不使用NIB

1、重写自定义cell的initWithStyle:withReuseableCellIdentifier:方法进行布局

2、注册cell

[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];   

3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的class类创建cell并返回

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];   

4、获取cell时如果没有可重用的cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell

最新文章

  1. C# Enum,Int,String的互相转换 枚举转换
  2. ireport制作小技巧<Reproduce>
  3. WCF创建RESTService
  4. VMware桥接模式无法自动化获取IP的解决方法
  5. windows下使用mysql双机热备功能
  6. bzoj4514: [Sdoi2016]数字配对--费用流
  7. Bootstrap 轮播插件
  8. 经典信息图表:2013 扁平设计 VS 拟物设计
  9. 小知识~LocalDB在IIS上如何成功配置
  10. 转:DataTable.Compute()用法
  11. 使用 fn 标签 解决字数过多时用省略号代替 .............................
  12. [React] React Fundamentals: with-addons - ReactLink
  13. adb server is out of date. killing... 解决方案
  14. 出现Deprecated: Function ereg_replace() is deprecated in 的原因及解决方法
  15. 窗口缩小div内容隐藏看不到怎么解决?
  16. Hive-RCFile文件存储格式
  17. Spring Boot 返回 XML 数据,一分钟搞定!
  18. solr6.5.0(windows)教程
  19. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十二):解决跨域问题
  20. Pycharm快捷键大全(windows + Mac)

热门文章

  1. Python基础-修改excel、redis、接口开发、组织代码
  2. Linux命令第四篇
  3. poj3009 Curling 2.0(很好的题 DFS)
  4. delphi ListView 设置固定列宽
  5. CSS中margin边界叠加问题及解决方案
  6. IE中的console.log
  7. docker中的数据库
  8. 创建MySQL用户 赋予某指定库表的权限
  9. CentOS7开启docker远程访问并在idea中连接使用
  10. ZMQ示例:使用 curve 进行加密通信