ios中Tableview的两个获得重用cell方法的区别?(兼容iPhone5.0之前的版本)

RZMars个人Blog地址: http://www.rzmars.com

皆さん、こんにちは。把今晚遇到的问题贴出来。どうぞよろしくお願いします

在tableView: cellForRowAtIndexPath:方法中有两个可以获得重用cell的方法;
分别是:- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
// Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)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 regist
我们可以发现第二个方法的的注释已经说明这个方法是ios6.0之后才又的,那么我们低版本使用的话肯定会产生问题的了,不过还好抛出的异常已经告诉了我们答案:

//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’

很明显,做android开发的人一看到这个可能又点想法了,因为在android当中也又类似的模式,动态加载布局文件嘛!

那么我们应该如何在ios5.0之前取正确的使用这个方法呢?我们需要结合一下两个方法中的任一个去配套的完成这正确的使用这个方法,方法如下:

// 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);

这两个方法分别是使用加载类与nib文件的。我们需要在那里使用他们呢?
假如我们要自定义一个Tableview的话 我们可以在这个控间的init 或者viewdidload方法中去实现这个方法,代码如下:
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];

这样我们就可以在 tableview中的cellForRowAtIndexPath方法中调用这个- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier 
forIndexPath:(NSIndexPath *)indexPath这个方法了。

你的明白?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值