Tableview的4种注册方式

其实tableveiw只有两种注册方式,注册和非注册。但是因为有了xib所以两种方式里纯代码和xib又形成了两种写法,今天有时间总结到一起,方便大家看。

注册方式:

注册cell

    //向 tableView 注册 cell 的样式  如果是 纯代码的自定Cell 使用该方法
    [self.tableView registerClass:[ListCell class] forCellReuseIdentifier:@"listcell"];

    //向 tableView 注册 cell 的样式  这里需要tableView 根据 xib 帮我创建cell 对象
    UINib *listnib = [UINib nibWithNibName:@"ListCell" bundle:nil];
    [self.tableView registerNib:listnib forCellReuseIdentifier:@"listcell"];

复用cell

ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell" forIndexPath:indexPath];

非注册方式:

复用cell

  ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell"];
    if (!listCell) { 
   //xib方式
   listCell = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:nil options:nil].firstObject;
    //纯代码方式
    ListCell=[[ListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListCell"];
    }


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值