(0069)iOS开发之dequeueReusableCellWithIdentifier的两个方法的区别
来源:互联网 发布:c语言编程简单的贪吃蛇 编辑:程序博客网 时间:2024/03/29 15:48
参考:http://www.jianshu.com/p/ce0cdb1bf20b
:http://blog.csdn.net/mandmg/article/details/52456862
if (fobject) 是BOOL变量
if (fobj != nil)是指针或者是值
这就是规范。
- 如果你注册过Cell,在没有可用的cell时,前者会返回nil;而后者永远都会从注册的nib或者class中替你创建一个可用的Cell。也就是说,前者调用你需要手动检查nil,而后者不需要;
- 如果你从没有注册过cell,在没有可用的cell时,前者会返回nil,后者……直接崩溃!也就是说,调用后者你 必须确保注册过cell
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 iOS6, clients can register a nibor class for each cell.
// If all reuse identifiers are registered,use the newer -dequeueReusableCellWithIdentifier:forIndexPath:to guarantee that a cell instanceis returned.
// Instances returned from the new dequeue method will also be properly sizedwhen 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";
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
取而代之的是下面这句代码:UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];
一、使用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:需要判断返回的是否为空)
CustomCell*cell=[_tableView dequeueReusableCellWithIdentifier:kCellIdentifyforIndexPath:indexPath];
4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的awakeFromNib方法
二、不使用NIB
1、重写自定义cell的initWithStyle:withReuseableCellIdentifier:方法进行布局
2、注册cell [_tableViewregisterClass:[CustomCellclass] forCellReuseIdentifier:kCellIdentify];
3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的class类创建cell并返回CustomCell*cell=[tableView dequeueReusableCellWithIdentifier:kCellIdentifyforIndexPath:indexPath];
4、获取cell时如果没有可重用的cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell
- (0069)iOS开发之dequeueReusableCellWithIdentifier的两个方法的区别
- tableview的两个获得重用cell方法dequeueReusableCellWithIdentifier的区别
- dequeueReusableCellWithIdentifier和dequeueReusableCellWithIdentifier:forIndexPath的区别
- dequeueReusableCellWithIdentifier: 与dequeueReusableCellWithIdentifier: forIndexPath:的区别
- iOS-UITableView的两种重用Cell方法的区别(dequeueReusableCellWithIdentifier)
- iOS-UITableView的两种重用Cell方法的区别(dequeueReusableCellWithIdentifier)
- 移动开发:dequeueReusableCellWithIdentifier的运行机制
- IOS开发之NSDictionary取值的两个方法objectForKey与valueForKey的区别
- dequeueReusableCellWithIdentifier:和dequeueReusableCellWithIdentifier: forIndexPath:的问题
- dequeueReusableCellWithIdentifier的运行机制
- dequeueReusableCellWithIdentifier的理解
- dequeueReusableCellWithIdentifier的运行机制
- dequeueReusableCellWithIdentifier的理解
- iOS学习之—— dequeueReusableCellWithIdentifier
- -[UITableView dequeueReusableCellWithIdentifier:] 的意义何在?
- 对于dequeueReusableCellWithIdentifier:的个人理解
- UITableView 关于dequeueReusableCellWithIdentifier的问题
- 对于dequeueReusableCellWithIdentifier:的个人理解
- Zend Studio使用教程之在Zend Studio中调试PHP(1/5)
- 数据统计分析资料汇总
- 为什么重写equals时必须重写hashCode方法
- web前端面试知识点总结(1)
- connect 连接超时
- (0069)iOS开发之dequeueReusableCellWithIdentifier的两个方法的区别
- Android知识点整体目录
- 网上流行护眼色的RGB值和颜色代码
- Fastlane构建ipa包流程小结 -- match 篇
- Android 开发之 View.post() 的作用
- 运行script_faster_rcnn_VOC2007_ZF.m出现的错误
- AI 学习之路——轻松初探 Python 篇(一)
- FastJson一些常见方法(API)
- 数据结构实验之图论二:图的深度遍历