iOS 5 Storyboard Custom Cell Crash: UITableView dataSource must return a cell

﹥>﹥吖頭↗ 提交于 2019-12-11 18:06:27

问题


This is either an XCode bug, or me missing a crucial rule here.

Update: - What's the chance of this being a weird bug in XCode/Storyboard?

Situation:

  • iOS 5, Storyboard
  • This is the storyboard setup: http://i.imgur.com/T5WyD.png
  • Another screenshot of the full setup: http://i.imgur.com/1tVuz.png
  • TableViewController with Custom Cell, cell has reusable identifier "NewCell"
  • in "cellForRowAtIndexPath" I basically have:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"]; return cell;

  • This throws an exception:

    Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Things I have already tried:

  • I setup a new project from scratch, TabBarController, with a TableViewController and a Custom Cell, all wired up in Storyboard, set a reusable cell identifier. Used the same code as above, worked perfectly. I don't know why it didn't work with the storyboard above...

I have a feeling it has something to do with the tree I built there, which is a TabBarController, loading a NavigationController, loading a TableViewController, providing a few items, one is clicked, which loads another TableViewController, which is unable to work with the custom cell, somehow.

Important: - The issue is that the Storyboard should make sure that: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"]; will never return NIL (unlike without the Storyboard/iOS4). But, mine is nil. And I can't, for the hell of it, figure out what's happening.


回答1:


Heyo, I just had this problem today and figured out a few possible causes. To link a UITableView as a subview of a ViewController in Story board check that you have done these steps.

  1. In your "ViewController.h", add <UITableViewDataSource> to your list of protocols

    @interface ViewController : UIViewController <UITableViewDataSource>

    you might want to add <UITableViewDelegate> as well but I didn't.

  2. In your "ViewController.m" set up your Table view data source functions

    #pragma mark - Table view data source
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [yourCells count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        NewCell *cell = (NewCell *)[tableView 
                                               dequeueReusableCellWithIdentifier:@"NewCell"];
        if (cell == nil) {
            NSLog(@"Cell is NIL");
            cell = [[CustomCell alloc] 
                    initWithStyle:UITableViewCellStyleDefault 
                    reuseIdentifier:CellIdentifier];
        }
    
        return cell;
    
    }
  3. In Storyboard, connect the UITableView's "Data Source" and "Delegate" references to the ViewController.

  4. In Storyboard, give the Prototype Cell the Identifier "NewCell"

If these are done, I believe it should work.

Hope this helps =)




回答2:


I always use custom cells in the storyboard like this:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
return cell;

Make sure the number of rows/sections is at least one. And another thing to check is that you set the UITableViewController to your custom class name in the storyboard.




回答3:


  • Did you double check the cell prototype identifier is "NewCell"?
  • Your setup as Tabbar->navigationcontroller->tableview is perfectly fine. I am using this layout all the time.
  • verify also there's no useless runtime attributes anywhere in this
    viewcontroller/tableview/cell.
  • your prototype looks a bit weird there - you added a UIButton to the cell as subview? why?



回答4:


To solve this problem add the following just above your dequeueReusableCellWithIdentifier statement:

static NSString *CellIdentifier = @"NewCell";

making sure the identifier matches the prototype cell in Storyboard




回答5:


It maybe late to answer this but i bet this will fix all your problems



来源:https://stackoverflow.com/questions/25211632/uitableview-with-parse-error-help-nsinternalinconsistencyexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!