Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

前端 未结 22 1067
长情又很酷
长情又很酷 2020-11-22 10:10

So I was making an rss reader for my school and finished the code. I ran the test and it gave me that error. Here is the code it\'s referring to:

- (UITableV         


        
相关标签:
22条回答
  • 2020-11-22 10:57

    I'll just add that Xcode 4.5 includes the new dequeueReusableCellWithIdentifier:forIndexPath:
    in its default template code - a potential gotcha for developers expecting the older dequeueReusableCellWithIdentifier: method.

    0 讨论(0)
  • 2020-11-22 10:57

    I give you the answer in both Objective C and Swift.Before that I want to say

    If we use the dequeueReusableCellWithIdentifier:forIndexPath:,we must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method as Apple Documnetation Says

    So we add registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier:

    Once we registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

    in viewDidLoad method we should register the cell

    Objective C

    OPTION 1:

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

    OPTION 2:

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

    in above code nibWithNibName:@"CustomCell" give your nib name instead of my nib name CustomCell

    SWIFT

    OPTION 1:

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    

    OPTION 2:

    tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") 
    

    in above code nibName:"NameInput" give your nib name

    0 讨论(0)
  • 2020-11-22 11:03

    Although this question is fairly old, there is another possibility: If you are using Storyboards, you simply have to set the CellIdentifier in the Storyboard.

    So if your CellIdentifier is "Cell", just set the "Identifier" property: enter image description here

    Make sure to clean your build after doing so. XCode sometimes has some issues with Storyboard updates

    0 讨论(0)
  • 2020-11-22 11:03

    The problem is most likely because you config custom UITableViewCell in storyboard but you do not use storyboard to instantiate your UITableViewController which uses this UITableViewCell. For example, in MainStoryboard, you have a UITableViewController subclass called MyTableViewController and have a custom dynamic UITableViewCell called MyTableViewCell with identifier id "MyCell".

    If you create your custom UITableViewController like this:

     MyTableViewController *myTableViewController = [[MyTableViewController alloc] init];
    

    It will not automatically register your custom tableviewcell for you. You have to manually register it.

    But if you use storyboard to instantiate MyTableViewController, like this:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    MyTableViewController *myTableViewController = [storyboard  instantiateViewControllerWithIdentifier:@"MyTableViewController"];
    

    Nice thing happens! UITableViewController will automatically register your custom tableview cell that you define in storyboard for you.

    In your delegate method "cellForRowAtIndexPath", you can create you table view cell like this :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    //Configure your cell here ...
    
    return cell;
    }
    

    dequeueReusableCellWithIdentifier will automatically create new cell for you if there is not reusable cell available in the recycling queue.

    Then you are done!

    0 讨论(0)
  • 2020-11-22 11:03

    If you are going with Custom Static Cells just comment this method:

    //- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    static NSString *CellIdentifier = @"notificationCell";
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    //    return cell;
    //}
    

    and give the cells an identifier at "Attributes Inspector" in storyboard.

    0 讨论(0)
  • 2020-11-22 11:03

    Ran into this error bc cell re-use identifier was wrong-- a rookie mistake but it happens: 1. Makes SURE cell re-use idenifier has no misspellings or missing letters. 2. Along same line, don't forget capitalization counts. 3. Zeroes are not "O"s (Ohs)

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