通过xib加载UITableViewCell的新方式

我们以前通常会这样做

- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     static  NSString  *CellIdentiferId = @"MomentsViewControllerCellID";
     MomentsCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
     if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MomentsCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];
        

      };
        
     }
     return cell;
}

现在我们可以这么用
采用registerNib的方式,并且把设置都放在了willDisplayCell方法中了,而不是以前我们经常用的cellForRowAtIndexPath
这个方法我试了下,如果我们在xib中设置了 Identifier,那么此处的必须一致,否则会crash的

- (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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,118评论 3 38
  • //设置行高 (CGFloat)tableView:(UITableView *)tableView height...
    俊月阅读 1,318评论 0 1
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 1,004评论 0 1
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 595评论 0 0
  • 高中三年同班同学,没想到生日只差一天!不惑之年,聚聚很! 这个虎嘟嘟的小女孩是我的二宝,但这脸上天然没褶子...
    赵国兴阅读 263评论 0 0