Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

前端 未结 22 1102
长情又很酷
长情又很酷 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 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!

提交回复
热议问题