The most important difference is that the forIndexPath: version asserts (crashes) if you didn't register a class or nib for the identifier. The older (non-forIndexPath:) version returns nil in that case.

You register a class for an identifier by sending registerClass:forCellReuseIdentifier: to the table view. You register a nib for an identifier by sending registerNib:forCellReuseIdentifier:to the table view.

If you create your table view and your cell prototypes in a storyboard, the storyboard loader takes care of registering the cell prototypes that you defined in the storyboard.

Session 200 - What's New in Cocoa Touch from WWDC 2012 discusses the (then-new) forIndexPath: version starting around 8m30s. It says that “you will always get an initialized cell” (without mentioning that it will crash if you didn't register a class or nib).

The video also says that “it will be the right size for that index path”. Presumably this means that it will set the cell's size before returning it, by looking at the table view's own width and calling your delegate's tableView:heightForRowAtIndexPath: method (if defined).  This is why it needs the index path.

 
  • 博客分类:
  • iOS

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

Ios代码  
  1. - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
  2. - (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:的话,必须和下面的两个配套方法配合起来使用:

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

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

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

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

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

Ios代码  
  1. static NSString *CellIdentifier = @"Cell";
  2. if (cell == nil)
  3. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

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

Ios代码  
  1. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

一、使用NIB

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

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

Ios代码  
  1. [_tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];

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

Ios代码  
  1. CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

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

二、不使用NIB

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

2、注册cell

Ios代码  
  1. [_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];

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

Ios代码  
  1. CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
 
4、获取cell时如果没有可重用的cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell

转自:

http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi

http://eric-gao.iteye.com/blog/2234047

When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath的更多相关文章

  1. UITableView学习之辨析两个方法:⓵dequeueReusableCellWithIdentifier与⓶dequeueReusableCellWithIdentifier:forIndexPath:

    使用storyboard显示UITableView时,如果不修改系统默认生成的tableView:cellForRowAtIndexPath:方法中的代码,必须为UITableViewCell注册(填 ...

  2. dequeueReusableCellWithIdentifier 与 dequeueReusableCellWithIdentifier:forIndexPath 区别

    参考:http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequ ...

  3. [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 后面forIndexPath:indexPath参数的解释

    解决以下错误: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'u ...

  4. iOS在UITableViewController里使用UISearchDisplayController报错"[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:]"

    出现如下错误: 2016-02-13 22:09:22.318 Test[2757:192106] *** Assertion failure in -[UISearchResultsTableVie ...

  5. iOS UITableView 引起的崩溃问题

    其实 UITableView 应该是在iOS开发中使用最频繁的一个控件,一次同事之间聊天玩笑的说“一个页面,要是没使用UITableView,就好像称不上是一个页面”.虽然是个最常见的控件,但是他的强 ...

  6. 你真的了解UITableViewCell重用吗?

    一:首先查看一下关于UITableViewCell重用的定义 - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentif ...

  7. UITableViewCell重用的问题

    UITableView中有两种重用Cell的方法: - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequ ...

  8. IOS开发UI基础UITableView的属性

    UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSour ...

  9. IOS之未解问题--给UITableView提取UITableViewDataSource并封装瘦身失败

    前言:阅读了<更轻量的 View Controllers>,发现笔者这个优化重构代码的想法真的很不错,可以使得抽取的UITableViewDataSource独立写在一个类文件里,并且也写 ...

随机推荐

  1. 学习IT资源分享,欢迎各位知道的学习IT资源前来分享

    1:排序不是按照名次,随机排序 慕课学习网https://www.imooc.com/ 腾讯课堂 https://ke.qq.com/ 柠檬学院 http://bjlemon.edusoho.cn/ ...

  2. 前端开发-Weex初试

    1 Weex介绍 weex是阿里2016年开源的一套跨移动端(Andriod/IOS/Wap)的前端框架,采用VUE,较React Native入门相对简单 官网地址 2 Weex安装与初始化 2.1 ...

  3. MongoDB学习笔记——索引管理

    索引 索引能够提升查询的效率.没有索引,MongoDB必须扫描集合中的所有文档,才能找到匹配查询语句的文档. 索引是一种特殊的数据结构,将一小块数据集保存为容易遍历的形式.索引能够存储某种特殊字段或字 ...

  4. 重装windows7企业版时提示“安装程序无法创建新的系统分区,也无法定位现有系统

    第一步:把win7镜像发在你电脑的非系统盘的其他硬盘上. 第二步:重启机器,通过U 盘启动.进入win pe系统,关于这点我说一下,有些朋友也许不知道什么叫win pe系统,这个win pe 究竟有什 ...

  5. 微信公共账号开发:48001 api unauthorized解决

    微信公众平台开发:获取token成功并且拉取自定义菜单也OK,但是在拉取用户信息和分组信息 是总是出现这个 {"errcode":48001,"errmsg": ...

  6. RPC和NFS

    参考:http://eduunix.ccut.edu.cn/index2/html/linux/OReilly.SUSE.Linux.Jul.2006/059610183X/suselinux-CHP ...

  7. Python 内置彩蛋

    The Zen of Python, by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Simp ...

  8. assertThat用法

    一般匹配符1.assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) ); 注释: allOf匹配符表明如果接下来的所有条件必须 ...

  9. [转]mongodb与mysql相比的优缺点

    原文地址:http://blog.sina.com.cn/s/blog_966e430001019s8v.html 与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问 ...

  10. activemq安装与简单消息发送接收实例

    安装环境:Activemq5.11.1, jdk1.7(activemq5.11.1版本需要jdk升级到1.7),虚拟机: 192.168.147.131 [root@localhost softwa ...