iOS UITableView 裏的Cell用Xib實現

#pragma cell的內容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    TableViewCell *cell;

    //定義CustomCell的複用標識,這個就是剛纔在CustomCell.xib中設置的那個Identifier,一定要相同,否則無法複用

    static NSString *identifier =@"Cell";

    //根據複用標識查找TableView裏是否有可複用的cell,有則返回給cell

    cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];

    //判斷是否獲取到複用cell,沒有則從xib中初始化一個cell

    if (!cell) {

        //Custom.xib中的所有對象載入

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:nil options:nil];

        //第一個對象就是CustomCell

        cell = [nib objectAtIndex:0];

    }

    

    return cell;

}


發佈了45 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章