zoukankan      html  css  js  c++  java
  • When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath

      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

  • 相关阅读:
    SQL Server身份验证登录失败
    课程总结及加分项
    导入并配置Guns框架
    python数据化中文是方块显示
    服务外包平台测试
    idea配置javap
    interface和abstract的区别
    简记Vue弹窗组件eldaolog被父界面创建后,子界面created函数只调用一次的解决方案
    Vue computed属性和methods区别
    记录一下前端查询条件对应后端多个条件的一种简单粗暴解决方法
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/5481314.html
Copyright © 2011-2022 走看看