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.app

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.ide

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).this

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..net

 

 
  • 博客分類: 
  • iOS

UITableView中有兩種重用Cell的方法:prototype

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出來。code

 

使用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);  

 

一、若是是用NIB自定義了一個Cell,那麼就調用registerNib:forCellReuseIdentifier:blog

二、若是是用代碼自定義了一個Cell,那麼就調用registerClass:forCellReuseIdentifier:get

 

以上這兩個方法能夠在建立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];  

 

1、使用NIB

一、xib中指定cell的Class爲自定義cell的類型(不是設置File's Owner的Class)

二、調用registerNib:forCellReuseIdentifier:向數據源註冊cell

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

三、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:獲取重用的cell,若是沒有重用的cell,將自動使用提供的nib文件建立cell並返回(若是使用dequeueReusableCellWithIdentifier:須要判斷返回的是否爲空)

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

四、獲取cell時若是沒有可重用cell,將建立新的cell並調用其中的awakeFromNib方法

 

2、不使用NIB

一、重寫自定義cell的initWithStyle:withReuseableCellIdentifier:方法進行佈局

二、註冊cell

Ios代碼   收藏代碼
  1. [_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];   

三、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:獲取重用的cell,若是沒有重用的cell,將自動使用提供的class類建立cell並返回

Ios代碼   收藏代碼
  1. CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];   
 
四、獲取cell時若是沒有可重用的cell,將調用cell中的initWithStyle:withReuseableCellIdentifier:方法建立新的cell

轉自:

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

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

相關文章
相關標籤/搜索