iOS - UITableView中有兩種重用Cell的方法

來源:互聯網
上載者:User

標籤:沒有   class類   span   ORC   col   gis   str   複用   return   

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

iOS代碼 
- (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:的話,必須和下面的兩個配套方法配合起來使用:

iOS代碼 
// Beginning in iOS 6, clients can register a nib or class for each cell.  // If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.  // Instances returned from the new dequeue method will also be properly sized when 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:方法中就可以省掉下面這些代碼:

iOS代碼 
static NSString *CellIdentifier = @"Cell";  

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

if (cell == nil)  {    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  }

 

 

取而代之的是下面這句代碼:

iOS代碼 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];  

  

一、使用NIB

1、xib中指定cell的Class為自訂cell的類型(不是設定File‘s Owner的Class)

2、調用registerNib:forCellReuseIdentifier:向資料來源註冊cell

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

 

3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:擷取重用的cell,如果沒有重用的cell,將自動使用提供的nib檔案建立cell並返回如果使用dequeueReusableCellWithIdentifier:需要判斷返回的是否為空白

iOS代碼 
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];  

 

4、擷取cell時如果沒有可重用cell,將建立新的cell並調用其中的awakeFromNib方法

 

二、不使用NIB

1、重寫自訂cell的initWithStyle:withReuseableCellIdentifier:方法進行布局

2、註冊cell

iOS代碼
[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];   

 

3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:擷取重用的cell,如果沒有重用的cell,將自動使用提供的class類建立cell並返回

iOS代碼 
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];   

 

4、擷取cell時如果沒有可重用的cell,將調用cell中的initWithStyle:withReuseableCellIdentifier:方法建立新的cell

 

iOS - UITableView中有兩種重用Cell的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.