你真的懂得tableview cell的两个重用方法嘛

 

先说一下tableview  cell的两个重用方法(一下是iOS的描述)


- (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


苹果官方文档解释的已经很清楚了,但是使用的时候你做到不报错了么

1.第二个方法是在5.0及5.0之前的版本上是运行不起来的

2.使用第二个时候会报错误:reason: 'unable to dequeue cell with identifier Cell must register nib or class for the identifier or connect prototype cell in storyboard'

通过解释我们可以看出xcode让注册一个nib或者class,方法如下:

- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

这样当实现cell的dataSource方法时,就不需要写下面这么多了

TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if(!cell)

    {

        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

只需要

TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

原因是如果所有的重用标识符被注册了,通过dequeueReusableCellWithIdentifier:来返回cell的实例

其实原理两个是一样的,只能说ios6之后更简洁了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值