TableViewCell注册以及我遇到的一些简单的坑

1、系统注册cell的两种方式

【1】、在viewDidLoad 方法里面注册 
 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
【2】、系统推荐用这个方法,这个方法在某些情况下更加能避免循环复用(像你在cell里面修改某个控件内容的时候)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  
  //如果队列中没有该类型cell,则会返回nil,这个时候就需要自己创建一个cell
 if (cell == nil) {
      
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
}

2、自定制cell

【1】,不使用xib的情况下

    <1>、[self.tableView registerClass:[xxxxCell class] forCellReuseIdentifier:@"cell"];
    xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  <2>、
 xxxxCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (cell==nil) {
      cell=[[xxxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

【2】在使用xib的情况下

<1>、
[tableView registerNib:[UINib nibWithNibName:@"xxxxViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
    xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
<2>、
  xxxxCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@“xxxxCell" owner:self options:nil]lastObject];
    }

在使用自定制cell的时候,如果不使用xib,那么要调一下方法

//创建cell的时候就会默认调用这个方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //在这里创建你自己的子控件
        self.iconView = [[UIImageView alloc] init];
        self.titleLabel = [[UILabel alloc] init];
        self.detailLabel = [[UILabel alloc] init];
        self.priceLabel = [[UILabel alloc] init];
        self.titleLabel.backgroundColor = [UIColor redColor];
        self.detailLabel.backgroundColor = [UIColor greenColor];
        self.priceLabel.backgroundColor = [UIColor cyanColor];
        
        //添加子控件
        [self.contentView addSubview:self.iconView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.detailLabel];
        [self.contentView addSubview:self.priceLabel];
    }
    return self;
}


//即将布局子控件就会调用这个方法,我们在这里完成cell里面子控件的相对布局
- (void)layoutSubviews
{
    //重写这个方法,一定要记得手动调用父类方法。
    [super layoutSubviews];
     
}

在使用xib的时候直接拉控件就可以啦

当一个文件中有多个tableView的时候每一个cell注册一定要使用不同的cellID,不然会报错,还有如果用xib在cell上拉控件的时候要注意不要不小心把控件拉到cell的外边,不然也会报错。报错信息别为
instantiated view controller with identifier "UIViewController-BYZ-38-t0r" from storyboard "Main", but didn't get a UITableView.'

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

推荐阅读更多精彩内容

  • 1.类扩展和分类的区别 类扩展:没有名字可以为某个类增加额外的属性、成员变量和方法 分类:有名字只能扩充方法,不能...
    彼岸的黑色曼陀罗阅读 3,685评论 0 1
  • 7、不使用IB是,下面这样做有什么问题? 6、请说说Layer和View的关系,以及你是如何使用它们的。 1.首先...
    AlanGe阅读 4,310评论 0 1
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,386评论 30 472
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 10,236评论 1 9
  • 代码创建UIWindow对象 Xcode7之后使用代码创建UIWindow对象: //创建UIWindow对象 s...
    云之君兮鹏阅读 5,208评论 0 2