在iOS中有兩種創(chuàng)建cell的方式
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
// Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
在iOS 6中dequeueReusableCellWithIdentifier:被dequeueReusableCellWithIdentifier:forIndexPath:所取代油额。如此一來,在表格視圖中創(chuàng)建并添加UITableViewCell對象會變得更為精簡而流暢。而且使用dequeueReusableCellWithIdentifier:forIndexPath:一定會返回cell莱革,系統(tǒng)在默認(rèn)沒有cell可復(fù)用的時候會自動創(chuàng)建一個新的cell出來。
使用dequeueReusableCellWithIdentifier:forIndexPath:的話,必須和下面的兩個配套方法配合起來使用:
// 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:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
1、如果是用NIB自定義了一個Cell迫皱,那么就調(diào)用registerNib:forCellReuseIdentifier:
2、如果是用代碼自定義了一個Cell辖众,那么就調(diào)用registerClass:forCellReuseIdentifier:
以上這兩個方法可以在創(chuàng)建UITableView的時候進行調(diào)用卓起。
這樣在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的類型(不是設(shè)置File's Owner的Class)
2凹炸、調(diào)用registerNib:forCellReuseIdentifier:向數(shù)據(jù)源注冊cell
[_tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];
3戏阅、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:獲取重用的cell,如果沒有重用的cell啤它,將自動使用提供的nib文件創(chuàng)建cell并返回(如果使用dequeueReusableCellWithIdentifier:需要判斷返回的是否為空)
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
4奕筐、獲取cell時如果沒有可重用cell,將創(chuàng)建新的cell并調(diào)用其中的awakeFromNib方法
二蚕键、不使用NIB
1救欧、重寫自定義cell的initWithStyle:withReuseableCellIdentifier:方法進行布局
2衰粹、注冊cell
[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];
3锣光、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:獲取重用的cell,如果沒有重用的cell铝耻,將自動使用提供的class類創(chuàng)建cell并返回
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
4誊爹、獲取cell時如果沒有可重用的cell,將調(diào)用cell中的initWithStyle:withReuseableCellIdentifier:方法創(chuàng)建新的cell