UITableView的兩種重用Cell方法的區(qū)別

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

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);  

在iOS 6中dequeueReusableCellWithIdentifier:被dequeueReusableCellWithIdentifier:forIndexPath:所取代。如此一來(lái),在表格視圖中創(chuàng)建并添加UITableViewCell對(duì)象會(huì)變得更為精簡(jiǎn)而流暢。而且使用dequeueReusableCellWithIdentifier:forIndexPath:一定會(huì)返回cell,系統(tǒng)在默認(rèn)沒(méi)有cell可復(fù)用的時(shí)候會(huì)自動(dòng)創(chuàng)建一個(gè)新的cell出來(lái)。

使用dequeueReusableCellWithIdentifier:forIndexPath:的話,必須和下面的兩個(gè)配套方法配合起來(lái)使用:
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自定義了一個(gè)Cell,那么就調(diào)用registerNib:forCellReuseIdentifier:
2、如果是用代碼自定義了一個(gè)Cell,那么就調(diào)用registerClass:forCellReuseIdentifier:

以上這兩個(gè)方法可以在創(chuàng)建UITableView的時(shí)候進(jìn)行調(diào)用。

這樣在tableView:cellForRowAtIndexPath:方法中就可以省掉下面這些代碼:
Ios代碼

static NSString *CellIdentifier = @"Cell";  
if (cell == nil)   
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  

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

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

一、使用NIB
1、重寫自定義cell的awakeFromNib方法進(jìn)行布局
2、xib中指定cell的Class為自定義cell的類型(不是設(shè)置File's Owner的Class)
3、調(diào)用registerNib:forCellReuseIdentifier:向數(shù)據(jù)源注冊(cè)cell
Ios代碼
[_tableView registerNib [UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];

4、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:獲取重用的cell,如果沒(méi)有重用的cell,將自動(dòng)使用提供的nib文件創(chuàng)建cell并返回(如果使用dequeueReusableCellWithIdentifier:需要判斷返回的是否為空)
Ios代碼

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

5、獲取cell時(shí)如果沒(méi)有可重用cell,將創(chuàng)建新的cell并調(diào)用其中的awakeFromNib方法

二、不使用NIB
1、重寫自定義cell的initWithStyle:withReuseableCellIdentifier:方法進(jìn)行布局
2、注冊(cè)cell
Ios代碼

[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];   

3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:獲取重用的cell,如果沒(méi)有重用的cell,將自動(dòng)使用提供的class類創(chuàng)建cell并返回
Ios代碼

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

4、獲取cell時(shí)如果沒(méi)有可重用的cell,將調(diào)用cell中的initWithStyle:withReuseableCellIdentifier:方法創(chuàng)建新的cell

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容