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. error C3861: “LOG4CPLUS_DEBUG”: 找不到标识

    头文件#include <log4cplus/loggingmacros.h>解决问题

  2. javaweb学习总结(十六)——JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  3. Maven学习总结(九)——使用Nexus搭建Maven私服

    一.搭建nexus私服的目的 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建n ...

  4. C++多线程开发之actor model

    最近想把写过的一个多线程程序整理一下,这个程序主要特点是有一系列的互相之间有依赖关系的task.于是在网上找相关类库 1,一类是简单的线程池了,这也是原本俺的做法.之前使用的是手工调度,代码实现的很蛋 ...

  5. [C] c99int(让VC等编译器自动兼容C99的整数类型)V1.02。源码托管到github、添加CMake编译配置文件、使用doxygen规范注释

    新版本—— http://www.cnblogs.com/zyl910/p/zlstdint_v100.html[C] zlstdint(让VC.TC等编译器自动兼容C99的整数类型)V1.0.支持T ...

  6. TCP : two different sockets sharing a port?

    A server socket listens on a single port. All established client connections on that server are asso ...

  7. Windows下Nginx的安装与配置(转)

    一.首先去官网下载 nginx1.0.11的Windows版本,官网下载:http://nginx.org/download/nginx-1.0.11.zip 下载到软件包后,解压 nginx-ngi ...

  8. 和View Controllers一起工作

    在这一课中,你会继续在FoodTracker菜谱的场景工作.你会重新安排现有的UI元素并使用图像采集器添加到照片用户界面.当你完成,你的应用程序将是这个样子: 学习目标 在课程结束时,你将能够: 了解 ...

  9. HttpURLConnection GET/POST写法

    现在虽然HttpClient很好使,但也有人在用最原生的HttpURLConnection, 记录一下,备忘之. public class HttpUrlConnect { //get请求 publi ...

  10. android Animation 动画绘制逻辑

    参考:http://www.jianshu.com/p/3683a69c38ea 1.View.draw(Canvas) 其中步骤为:/* * Draw traversal performs seve ...