1.xib,nib拖控件:awakefromnib: 設(shè)置
2,不拖控件:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { //創(chuàng)建子控件 //頭像 UIImageView *iconView = [[UIImageView alloc] init]; [self.contentView addSubview:iconView]; self.iconView = iconView; //昵稱 UILabel *nameView = [[UILabel alloc] init]; [self.contentView addSubview:nameView]; self.nameView = nameView; // nameView.font = [UIFont systemFontOfSize:CZNameFont]; //會員 UIImageView *vipView = [[UIImageView alloc] init]; [self.contentView addSubview:vipView]; self.vipView = vipView; self.vipView.image = [UIImage imageNamed:@"vip"]; //微博內(nèi)容 UILabel *textView = [[UILabel alloc] init]; [self.contentView addSubview:textView]; self.textView = textView; textView.font = [UIFont systemFontOfSize:CZTextFont]; textView.numberOfLines = 0; //微博圖片 UIImageView *pictureView = [[UIImageView alloc] init]; [self.contentView addSubview:pictureView]; self.pictureView = pictureView; } return self; }
1、dequeueReuseableCellWithIdentifier:與dequeueReuseableCellWithIdentifier:forIndexPath:的區(qū)別:
前者不必向tableView注冊cell的Identifier,但需要判斷獲取的cell是否為nil;
后者則必須向table注冊cell,可省略判斷獲取的cell是否為空,因?yàn)闊o可復(fù)用cell時(shí)runtime將使用注冊時(shí)提供的資源去新建一個cell并返回
2、自定義cell時(shí),記得將其他內(nèi)容加到self.contentView 上,而不是直接添加到 cell 本身上
總結(jié):
1.自定義cell時(shí),
若使用nib,使用 registerNib: 注冊,dequeue時(shí)會調(diào)用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注冊, dequeue時(shí)會調(diào)用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
2.需不需要注冊?
使用dequeueReuseableCellWithIdentifier:可不注冊,但是必須對獲取回來的cell進(jìn)行判斷是否為空,若空則手動創(chuàng)建新的cell;
使用dequeueReuseableCellWithIdentifier:forIndexPath:必須注冊,但返回的cell可省略空值判斷的步驟。