iOS 注冊cell

2016.4.14

1.自定義cell時,

若使用nib,使用 registerNib: 注冊,dequeue時會調用 cell 的 -(void)awakeFromNib

不使用nib,使用 registerClass: 注冊, dequeue時會調用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:

文/XiaXiang(簡書作者)

原文鏈接:http://www.lxweimin.com/p/66420a87b844

著作權歸作者所有,轉載請聯系作者獲得授權,并標注“簡書作者”。

使用dequeueReuseableCellWithIdentifier:可不注冊,但是必須對獲取回來的cell進行判斷是否為空,若空則手動創建新的cell;

if(cell ==nil) {

cell = [[PreSallDetailCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cellid"];

}

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

xib:

[_tableView registerNib:[UINib nibWithNibName:@"xxxxxCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];

xxxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

代碼

[_tableView registerClass:[xxxxxCell class] forCellReuseIdentifier:kCellIdentify];

xxxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容