1. 程式人生 > >xib中建立cell的注意事項

xib中建立cell的注意事項

1、dequeueReuseableCellWithIdentifier:與dequeueReuseableCellWithIdentifier:forIndexPath:的區別:
前者不必向tableView註冊cell的Identifier,但需要判斷獲取的cell是否為nil;
後者則必須向table註冊cell,可省略判斷獲取的cell是否為空,因為無可複用cell時runtime將使用註冊時提供的資源去新建一個cell並返回
2、自定義cell時,記得將其他內容加到self.contentView 上,而不是直接新增到 cell 本身上

總結:
1.自定義cell時,
若使用nib,使用 registerNib: 註冊,dequeue時會呼叫 cell 的 -(void)awakeFromNib

不使用nib,使用 registerClass: 註冊, dequeue時會呼叫 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
2.需不需要註冊?
使用dequeueReuseableCellWithIdentifier:可不註冊,但是必須對獲取回來的cell進行判斷是否為空,若空則手動建立新的cell;

使用dequeueReuseableCellWithIdentifier:forIndexPath:必須註冊,但返回的cell可省略空值判斷的步驟。

http://www.cocoachina.com/bbs/read.php?tid=290160