iOS-UITableView的两种重用Cell方法的区别(dequeueReusableCellWithIdentifier)
2017-07-11 10:24
585 查看
转载自: http://blog.csdn.net/mandmg/article/details/52456862
UITableView中有两种重用Cell的方法:
2
1
2
在iOS 6中
一定会返回cell,系统在默认没有cell可复用的时候会自动创建一个新的cell出来。
使用
的话,必须和下面的两个配套方法配合起来使用:
2
3
4
5
1
2
3
4
5
1、如果是用NIB自定义了一个Cell,那么就调用
2、如果是用代码自定义了一个Cell,那么就调用
以上这两个方法可以在创建UITableView的时候进行调用。
这样在
2
3
1
2
3
取而代之的是下面这句代码
1
一、使用NIB
1、xib中指定cell的Class为自定义cell的类型(不是设置File’s Owner的Class)
2、调用
1
3、
1
4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的
二、不使用NIB
1、重写自定义cell的
2、注册cell
1
3、在
1
4、获取cell时如果没有可重用的cell,将调用cell中的
UITableView中有两种重用Cell的方法:
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);1
2
1
2
在iOS 6中
dequeueReusableCellWithIdentifier:被
dequeueReusableCellWithIdentifier:forIndexPath:所取代。如此一来,在表格视图中创建并添加UITableViewCell对象会变得更为精简而流畅。而且使用
dequeueReusableCellWithIdentifier:forIndexPath:
一定会返回cell,系统在默认没有cell可复用的时候会自动创建一个新的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:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0); - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);1
2
3
4
5
1
2
3
4
5
1、如果是用NIB自定义了一个Cell,那么就调用
registerNib:forCellReuseIdentifier:
2、如果是用代码自定义了一个Cell,那么就调用
registerClass:forCellReuseIdentifier:
以上这两个方法可以在创建UITableView的时候进行调用。
这样在
tableView:cellForRowAtIndexPath:方法中就可以省掉下面这些代码:
static NSString *CellIdentifier = @"Cell"; if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];1
2
3
1
2
3
取而代之的是下面这句代码
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];1
1
一、使用NIB
1、xib中指定cell的Class为自定义cell的类型(不是设置File’s Owner的Class)
2、调用
registerNib:forCellReuseIdentifier:向数据源注册cell
[_tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];1
1
3、
在tableView:cellForRowAtIndexPath:中使用
dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的nib文件创建cell并返回(如果使用
dequeueReusableCellWithIdentifier:需要判断返回的是否为空)
CustomCell*cell=[_tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];1
1
4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的
awakeFromNib方法
二、不使用NIB
1、重写自定义cell的
initWithStyle:withReuseableCellIdentifier:方法进行布局
2、注册cell
[_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];1
1
3、在
tableView:cellForRowAtIndexPath:中使用
dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的class类创建cell并返回
CustomCell*cell=[tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];1
1
4、获取cell时如果没有可重用的cell,将调用cell中的
initWithStyle:withReuseableCellIdentifier:方法创建新的cell
相关文章推荐
- iOS-UITableView的两种重用Cell方法的区别(dequeueReusableCellWithIdentifier)
- iOS UItableview的两种重用cell的方法的区别
- UITableView学习之辨析两个方法:⓵dequeueReusableCellWithIdentifier与⓶dequeueReusableCellWithIdentifier:forIndexPath:
- UITableView中的dequeueReusableCellWithIdentifier的方法
- iOS 坑点 Assertion failure in - UITableView dequeueReusableCellWithIdentifier:forIndexPath:
- iOS 运行出现:Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /Sourc
- UITableView的两种重用Cell方法的区别
- (0069)iOS开发之dequeueReusableCellWithIdentifier的两个方法的区别
- UITableView 重用(dequeueReusableCellWithIdentifier:)
- iOS 关于UITableView的dequeueReusableCellWithIdentifier
- UITableView 重用cell方法edequeueReusableCellWithIdentifier,出现错误
- ios tableView UITableView中Cell重用机制导致内容重复解决方法
- ios中Tableview的两个获得重用cell方法的区别?(兼容iPhone5.0之前的版本)
- *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot
- Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]
- iOS开发笔记--解决UITableView中Cell重用机制导致内容出错的方法总结
- ios tableView那些事 (十六) UITableView中Cell重用机制导致内容重复解决方法
- ios tableView那些事 (十六) UITableView中Cell重用机制导致内容重复解决方法
- ios UITableView中Cell重用机制导致内容重复解决方法
- Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]