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];