Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

前端 未结 22 1023
长情又很酷
长情又很酷 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:42

    I think this error is about registering your nib or class for the identifier.

    So that you may keep what you are doing in your tableView:cellForRowAtIndexPath function and just add code below into your viewDidLoad:

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

    It's worked for me. Hope it may help.

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

    I setup everything correctly in the Storyboard and did a clean build but kept getting the error " must register a nib or a class for the identifier or connect a prototype cell in a storyboard"

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

    Corrected the error but i'm still at a loss. I'm not using a 'custom cell', just a view with a tableview embeded. I have declared the viewcontroller as delegate and datasource and made sure the cell identifier matches in file. whats going on here?

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

    In my case, the crash happened when I calleddeselectRowAtIndexPath:

    The line was [tableView deselectRowAtIndexPath:indexPath animated:YES];

    Changing it to [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; FIXED MY PROBLEM!

    Hope this helps anyone

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

    FWIW, I got this same error when I forgot to set the cell identifier in the storyboard. If this is your issue then in the storyboard click the table view cell and set the cell identifier in the attributes editor. Make sure the cell identifier you set here is the same as

    static NSString *CellIdentifier = @"YourCellIdenifier";
    
    0 讨论(0)
  • 2020-11-22 10:46

    I spent hours last night working out why my programmatically generated table crashed on [myTable setDataSource:self]; It was OK commenting out and popping up an empty table, but crashed every time I tried to reach the datasource;

    I had the delegation set up in the h file: @interface myViewController : UIViewController

    I had the data source code in my implementation and still BOOM!, crash every time! THANK YOU to "xxd" (nr 9): adding that line of code solved it for me! In fact I am launching a table from a IBAction button, so here is my full code:

        - (IBAction)tapButton:(id)sender {
    
        UIViewController* popoverContent = [[UIViewController alloc]init];
    
        UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
        popoverView.backgroundColor = [UIColor greenColor];
        popoverContent.view = popoverView;
    
        //Add the table
        UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)         style:UITableViewStylePlain];
    
       // NEXT THE LINE THAT SAVED MY SANITY Without it the program built OK, but crashed when      tapping the button!
    
        [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        table.delegate=self;
        [table setDataSource:self];
        [popoverView addSubview:table];
        popoverContent.contentSizeForViewInPopover =
        CGSizeMake(200, 300);
    
        //create a popover controller
        popoverController3 = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];
        CGRect popRect = CGRectMake(self.tapButton.frame.origin.x,
                                    self.tapButton.frame.origin.y,
                                    self.tapButton.frame.size.width,
                                    self.tapButton.frame.size.height);
    
    
        [popoverController3 presentPopoverFromRect:popRect inView:self.view   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    
    
       }
    
    
       #Table view data source in same m file
    
       - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
       {
        NSLog(@"Sections in table");
        // Return the number of sections.
        return 1;
       }
    
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
      {
        NSLog(@"Rows in table");
    
        // Return the number of rows in the section.
        return myArray.count;
       }
    
       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
        {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        NSString *myValue;
    
        //This is just some test array I created:
        myValue=[myArray objectAtIndex:indexPath.row];
    
        cell.textLabel.text=myValue;
        UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 12.0 ];
        cell.textLabel.font  = myFont;
    
        return cell;
       }
    

    By the way: the button must be linked up with as an IBAction and as a IBOutlet if you want to anchor the popover to it.

    UIPopoverController *popoverController3 is declared in the H file directly after @interface between {}

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

    you have to be aware that when using interface builder and creating a Xib (nib) containing one cell that there is also a placeholder created that points to the class that will be used. Meaning, when you place two UITableViewCell in one Xib file you possibly run into the exact same trouble causing an *** Assertion failure .... The placeholder mechanism doesn't work adn will be confused then. Instead placing different placeholders in one Xib read on..

    The most easy solution (even if that seems a bit to simple) is to place one Cell at a time in one Xib. IB will create a placeholder for you and all works as expected then. But this then leads directly to one extra line of code, because then you need to load the correct nib/xib asking for the reuseIdentified Cell where it resides in. So the following example code focuses the use of multiple Cell Indentifiers in one tableview where an Assertion Failure is very common.

    // possibly above class implementation
    static NSString *firstCellIdentifier = @"firstCellIdentifier";
    static NSString *secondCellIdentifier = @"secondCellIdentifier";
    
    // possibly in -(instancetype)init 
    UINib *firstNib = [UINib nibWithNibName:@"FirstCell" bundle:nil];
    [self.tableView registerNib:firstNib forCellReuseIdentifier:firstCellIdentifier];
    UINib *secondNib = [UINib nibWithNibName:@"SecondCell" bundle:nil];
    [self.tableView registerNib:secondNib forCellReuseIdentifier:secondCellIdentifier];
    

    Another trouble with the use of two CellIdentifier's in one UITableView is that row height and/or section height have to be taken care of. Two cells can have different heights of course.

    When registering classes for reuse the code should look different.

    The "simple solution" looks also much different when your cells reside inside a Storyboard instead of Xib's. Watch out for the placeholders.

    Keep also in mind that interface builder files have version wise variations and need to be set to a version that your targeted OS version supports. Even if you may be lucky that the particular feature you placed in your Xib is not changed since the last IB version and does not throw errors yet. So a Xib made with IB set to be compatible to iOS 13+ but used in a target that is compiled on an earlier version iOS 12.4 will cause also trouble and can end up with Assertion failure.

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