cell的注册重用问题

 cell 的注册重用与 cell 的创建方式息息相关,现整理了几种,希望对大家有帮助!

1. cell 通过 storyboard 、代码创建

1.1通过 storyboard 创建时,即在 storyboard 中创建 tableView,在 tableView 中直接添加 cell,添加几个 cell 代表有几种 cell,注册重用可以直接在 storyboard 中注册,直接上图!


1.2 通过代码创建,创建时后在 (以ViewController为例) ViewController.m 中注册,通过代码注册(上面通过 storyboard 创建的 cell 也可以通过这种方式 ,以代码注册,但是要注意,根据自测,在 storyboard 中注册过后,是不能通过代码修改 identifier 的)

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

     [self.tableView registerClass:[Cell class] forCellReuseIdentifier:@"cellZhuCe"];

}


2. 通过 xib 创建 cell 注册方法

2.1直接在 xib 中注册,直接上图



2.2 在代码中注册

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

   UINib * nib = [UINib nibWithNibName:NSStringFromClass([Cell class]) bundle:[NSBundle mainBundle]];

    [self.tableView registerNib:nib forCellReuseIdentifier:@"cellZhuCe"];

}


两种重用方法分享给大家

1.   

    NSString * ID = @"Items";

    GoodsCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];


    if (!cell) {

           cell = [[[NSBundle mainBundle] loadNibNamed:@"cellZhuCe" owner:nil options:nil]firstObject];

    }

这种方法,以上四种注册皆可用

2.

GoodsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellZhuCe" forIndexPath:indexPath];


这种方法代码简洁,但是在使用时应注意,在通过 xib 创建 cell 时,应该用代码注册(通过2.2的方法),直接在 xib 中注册,需要加载到ViewController里,但是怎么加载这么问题我没有解决,或者是别的原因?哪位朋友解决了,麻烦告知一声,万分感激!!!


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值