UITableView的registerClass forCellReuseIdentifier用法详解
2015-09-25 17:38
656 查看
tableView: cellForRowAtIndexPath:方法中有两个获得重用cell的方法
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
和
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
请问他们有什么区别?
当我用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]的时候为什么总报错
reason:
'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
------解决方案--------------------------------------------------------
- (id)dequeueReusableCellWithIdentifier:(NSString
*)identifier;
- (id)dequeueReusableCellWithIdentifier:(NSString
*)identifier forIndexPath:(NSIndexPath *)indexPath
NS_AVAILABLE_IOS(6_0);
// newer
区别在这儿
------解决方案--------------------------------------------------------
1 这个方法在SDK5.0是运行不起来的。
2 如果需要使用这个方法,你必须使用配套的方法来一起用,下面两个配套方法:
// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are reg
4000
istered, 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);
注意看上面的注释
3 比如你已经用NIB做了一个Cell,或者自定义了一个Cell。我们在你创建UITableView的时候,就可以顺带
self.tableView.backgroundColor = xxxx;
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
这样你在- (UITableViewCell *)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath这个方法里,你就可以省下这些代码:
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//设置你的cell
}
而只需要
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
这样就够了,这下你明白了吗?
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
和
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
请问他们有什么区别?
当我用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]的时候为什么总报错
reason:
'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
------解决方案--------------------------------------------------------
- (id)dequeueReusableCellWithIdentifier:(NSString
*)identifier;
- (id)dequeueReusableCellWithIdentifier:(NSString
*)identifier forIndexPath:(NSIndexPath *)indexPath
NS_AVAILABLE_IOS(6_0);
// newer
区别在这儿
------解决方案--------------------------------------------------------
1 这个方法在SDK5.0是运行不起来的。
2 如果需要使用这个方法,你必须使用配套的方法来一起用,下面两个配套方法:
// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are reg
4000
istered, 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);
注意看上面的注释
3 比如你已经用NIB做了一个Cell,或者自定义了一个Cell。我们在你创建UITableView的时候,就可以顺带
self.tableView.backgroundColor = xxxx;
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
这样你在- (UITableViewCell *)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath这个方法里,你就可以省下这些代码:
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//设置你的cell
}
而只需要
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
这样就够了,这下你明白了吗?
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- 基于UIControl控件实现ios点赞功能
- iOS开发中实现邮件和短信发送的简单示例