TableViewCell注冊以及我遇到的一些簡單的坑

1、系統(tǒng)注冊cell的兩種方式

【1】、在viewDidLoad 方法里面注冊 
 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
【2】、系統(tǒng)推薦用這個方法,這個方法在某些情況下更加能避免循環(huán)復(fù)用(像你在cell里面修改某個控件內(nèi)容的時候)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  
  //如果隊列中沒有該類型cell,則會返回nil,這個時候就需要自己創(chuàng)建一個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,那么要調(diào)一下方法

//創(chuàng)建cell的時候就會默認(rèn)調(diào)用這個方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //在這里創(chuàng)建你自己的子控件
        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;
}


//即將布局子控件就會調(diào)用這個方法,我們在這里完成cell里面子控件的相對布局
- (void)layoutSubviews
{
    //重寫這個方法,一定要記得手動調(diào)用父類方法。
    [super layoutSubviews];
     
}

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

當(dāng)一個文件中有多個tableView的時候每一個cell注冊一定要使用不同的cellID,不然會報錯,還有如果用xib在cell上拉控件的時候要注意不要不小心把控件拉到cell的外邊,不然也會報錯。報錯信息別為
instantiated view controller with identifier "UIViewController-BYZ-38-t0r" from storyboard "Main", but didn't get a UITableView.'

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.類擴(kuò)展和分類的區(qū)別 類擴(kuò)展:沒有名字可以為某個類增加額外的屬性、成員變量和方法 分類:有名字只能擴(kuò)充方法,不能...
    彼岸的黑色曼陀羅閱讀 663評論 0 1
  • 7、不使用IB是,下面這樣做有什么問題? 6、請說說Layer和View的關(guān)系,以及你是如何使用它們的。 1.首先...
    AlanGe閱讀 990評論 0 1
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,616評論 30 472
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,979評論 1 9
  • 代碼創(chuàng)建UIWindow對象 Xcode7之后使用代碼創(chuàng)建UIWindow對象: //創(chuàng)建UIWindow對象 s...
    云之君兮鵬閱讀 1,495評論 0 2

友情鏈接更多精彩內(nèi)容