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