Tableview的4種註冊方式

其實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"];
    }


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章