來源:http://blog.csdn.net/youngsblog/article/details/44536143
另外要注意的:
- dequeueReuseableCellWithIdentifier:與
dequeueReuseableCellWithIdentifier:forIndexPath:的區(qū)別:
前者不必向tableView注冊cell的Identifier,但需要判斷獲取的cell是否為nil;
后者則必須向table注冊cell,可省略判斷獲取的cell是否為空,因為無可復(fù)用cell時runtime將使用注冊時提供的資源去新建一個cell并返回
- 自定義cell時,記得將其他內(nèi)容加到self.contentView 上,而不是直接添加到 cell 本身上
總結(jié)
- 自定義cell時,
若使用nib,使用 registerNib: 注冊,dequeue時會調(diào)用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注冊, dequeue時會調(diào)用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier: - 需不需要注冊?
使用dequeueReuseableCellWithIdentifier:可不注冊,但是必須對獲取回來的cell進行判斷是否為空,若空則手動創(chuàng)建新的cell;
使用dequeueReuseableCellWithIdentifier:forIndexPath:必須注冊,但返回的cell可省略空值判斷的步驟。
Mark:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self == [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]) {
}
}