tableviewcell加载的几种方法

本文介绍了UITableViewCell的两种加载方法。一是通过Storyboard直接加载,无需初始化,关键在于正确设置identifier。二是使用XIB文件加载,需设置identifier并进行文件读取操作。

tableviewcell在加载的过程中常常需要重用,主要在使用的是两种加载的方法。第一种是在storyboard中直接加载,这种方法不需要进行初始化,可以直接使用。但是要注意identifier一定要设置,控制器是根据identifier来识别cell的。


第二种方法是用xib文件来加载,同样要设置identifier。不过多了从文件中读取xib的过程。代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:
@"myCell"];
    
if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:
@"MyCustomCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:
@"myCell"];
    }
    
    
return cell;
}

- (
void)tableView:(UITableView *)tableView willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.leftLabel.text = [
self.items objectAtIndex:indexPath.row];
    cell.rightLabel.text = [
self.items objectAtIndex:indexPath.row];
    cell.middleLabel.text = [
self.items objectAtIndex:indexPath.row];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值