UITableView registerNib:forCellReuseIdentifier:

前端 未结 3 1769
忘了有多久
忘了有多久 2020-12-23 14:18

I saw this used in a WWDC video but only very briefly. They didn\'t go in to how to create the actual xib file.

I\'ve got a UITableViewCell subclass called MyCustom

3条回答
  •  情话喂你
    2020-12-23 14:25

    I've found the code given in the question is fine, but you can't refer to self.tableView in the init method if you're using storyboards. There's some discussion about it in another question.

    So the first line goes in the init:

    self.cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];

    But this line should go in viewDidLoad or similar:

    [self.tableView registerNib:self.cellNib forCellReuseIdentifier:@"CustomCell"];

    That fixes my mysterious error, e.g. "*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'Ogf-Sj-1ej-view-bBf-Ti-Dda''"

    And, yes, I'm doing something very similar to scoop things out of Storyboards and place them in xibs for reuse across view controllers!

提交回复
热议问题