iOS - UITableView中有两种重用Cell的方法
- (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:的话,必须和下面的两个配套方法配合起来使用:
- (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:方法中就可以省掉下面这些代码:
//======================== 纯代码创建cell =============================
1.定义一个cell的标识
static NSString *CellIdentifier = @"Cell";
2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// 3.如果缓存池中没有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//=====================================================================
//======================== xib 创建cell =============================
1.定义一个cell的标识
static NSString *CellIdentifier =@"Cell";
BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([Cellclass]) bundle:nil];[tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
nibsRegistered = YES;
}
2.从缓存池中取出cell
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
1.UINib是一个IOS4.0才出现的类,与MAC上的NSNib类作用相似, 就是加速频繁使用的NIB文件的加载。在第一次从硬盘加载NIB时,它在内存中缓存NIB文件对象。之后加载NIB文件时就会从内存拷贝而避免了较慢的硬盘访问。Apple宣称可以在 加载NIB文件时提供2倍的速度提升。 使用UINib的最明显的地方就是在需要在每次创建新Cell时从NIB文件中加载Cell的UITableViewControllers中。UINib的优势就是在不用大量修改代码的情况获得性能改进。其实简单地说,就是利用缓存机制避免了频繁从硬盘中加载XIB文件,这在大数据量的时候显得尤为有用。
2.除了上述代码,还需要在xib文件中做如下设置:在Cell.xib的Inspector窗口中将Identifier进行设置,这里的Identifier要与cellForRowAtIndexPath中一致。
//=====================================================================
取而代之的是下面这句代码:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];
一、使用NIB
1、xib中指定cell的Class为自定义cell的类型(不是设置File's Owner的Class)
2、调用registerNib:forCellReuseIdentifier:向数据源注册cell
[_tableView registerNib:[UINib nibWithNibName:@"CustomCell"bundle:nil] forCellReuseIdentifier:kCellIdentify];
3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的nib文件创建cell并返回(如果使用dequeueReusableCellWithIdentifier:需要判断返回的是否为空)
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的awakeFromNib方法
二、不使用NIB
1、重写自定义cell的initWithStyle:withReuseableCellIdentifier:方法进行布局
2、注册cell
[_tableView registerClass:[CustomCellclass] forCellReuseIdentifier:kCellIdentify];
3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的class类创建cell并返回
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
4、获取cell时如果没有可重用的cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell
网友评论