其實tableveiw只有兩種註冊方式,註冊和非註冊。但是因爲有了xib所以兩種方式裏純代碼和xib又形成了兩種寫法,今天有時間總結到一起,方便大家看。
註冊方式:
註冊cell
//向 tableView 註冊 cell 的樣式 如果是 純代碼的自定Cell 使用該方法
[self.tableView registerClass:[ListCell class] forCellReuseIdentifier:@"listcell"];
//向 tableView 註冊 cell 的樣式 這裏需要tableView 根據 xib 幫我創建cell 對象
UINib *listnib = [UINib nibWithNibName:@"ListCell" bundle:nil];
[self.tableView registerNib:listnib forCellReuseIdentifier:@"listcell"];
複用cell
ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell" forIndexPath:indexPath];
非註冊方式:
複用cell
ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell"];
if (!listCell) {
//xib方式
listCell = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:nil options:nil].firstObject;
//純代碼方式
ListCell=[[ListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListCell"];
}