Why am I getting an error about being unable to dequeue when my UITableView tries to load?

前端 未结 4 1595
抹茶落季
抹茶落季 2020-12-30 04:27

I get the following error:

* Terminating app due to uncaught exception \'NSInternalInconsistencyException\', reason: \'unable to dequeue a ce

相关标签:
4条回答
  • 2020-12-30 05:05

    Easiest fix is to just change it to FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; As with your current code, you'll have to check to be sure cell is not nil if you do this method.


    Alternately, you can register a UINib or Class at the table level that is tied to @"FontCell"

    For example (up in viewDidLoad):

    [self.tableView registerClass: [FontCell class] forCellReuseIdentifier:@"FontCell"];
    

    Then you can do

    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FontCell" forIndexPath:indexPath];
    

    The nice thing with this method is that you know that your cell will never be nil, so you can just immediately begin modifying it.

    0 讨论(0)
  • 2020-12-30 05:12

    If using a tableview (not a table view controller) as I was, there are not cells that appear by default.

    In the storyboard select the tableview Open the Attributes inspector Change prototype cells from 0 to 1 Select the newly displayed table cell In the Attributes inspector set the Identitifier to "FontCell"

    0 讨论(0)
  • 2020-12-30 05:13

    You're using the dequeueReusableCellWithIdentifier:forIndexPath: method. The documentation for that method says this:

    You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
    

    So here

    [self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"];
    
    0 讨论(0)
  • 2020-12-30 05:15

    I also had such a problem, and the solution I found is:

    Go to the Project Navigator and select “ViewController.h”. Append

     <UITableViewDelegate, UITableViewDataSource>
    

    after “UIViewController”.

    0 讨论(0)
提交回复
热议问题