Custom UITableViewCell Not Using .xib (Most Likely Because of Flaw in init Method)

后端 未结 3 1561
温柔的废话
温柔的废话 2020-12-10 09:36

I subclassed the UITableViewCell in order to customize it, but I think I\'m missing something because: 1) It\'s not working and 2) There are a couple of things I\'m confused

3条回答
  •  借酒劲吻你
    2020-12-10 10:05

    The easiest way (available since iOS 5.0) to create a custom table view cell in a nib file is to use registerNib:forCellReuseIdentifier: in the table view controller. The big advantage is that dequeueReusableCellWithIdentifier: then automatically instantiates a cell from the nib file if necessary. You don't need the if (cell == nil) ... part anymore.

    In viewDidLoad of the table view controller you add

    [self.tableView registerNib:[UINib nibWithNibName:@"CueTableCell" bundle:nil] forCellReuseIdentifier:@"CueTableCell"];
    

    and in cellForRowAtIndexPath you just do

    CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CueTableCell"];
    // setup cell
    return cell;
    

    Cells loaded from a nib file are instantiated using initWithCoder, you can override that in your subclass if necessary. For modifications to the UI elements, you should override awakeFromNib (don't forget to call "super").

提交回复
热议问题