0

I am creating the table in the loadView method because I need to resize it, and I am creating the cells in a nib to make it easier to edit them.

I keep getting the error below that I was not getting when my table was created in the nib.

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath

Code I believe is causing the error.

- (void)loadView
{
    // Set the size for the table view
    CGRect tableViewRect = CGRectMake(0, 30, 320, 436);

    // Create a table viiew
    UITableView *tableView = [[UITableView alloc] initWithFrame:tableViewRect style:UITableViewStyleGrouped];
    tableView.delegate = self;  
    tableView.dataSource = self;    

    // This view contains the table view and the status bar
    UIView *totalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    [totalView addSubview:tableView];   
    self.view = totalView;
    [tableView release];

    [totalView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return cellOne;
        }
        else {
            return cellTwo;
        }
    }
    else if (indexPath.section == 1) {
        return cellThree;
    }
    else {
        return cellFour;
    }
}
Vikings
  • 2,527
  • 32
  • 45

2 Answers2

2

If you override -(void)loadView, the nib file will not be loaded. This is assuming that cellOne etc. are IBOutlet properties. If you want to create your table view manually, you should delete your -(void)loadView method, and move the code to -(void)viewDidLoad

Ell Neal
  • 6,014
  • 2
  • 29
  • 54
  • I can not believe I did not think of that, that's the whole point of that method. – Vikings Jan 27 '12 at 01:26
  • CGRect tableViewRect = CGRectMake(0, 30, 320, 436); the table is not that size – Vikings Jan 27 '12 at 01:29
  • Why can't you just lay it out in IB? There's no reason you can't specify that frame in a nib file. – Ell Neal Jan 27 '12 at 01:30
  • Actually I get his error loaded the "SettingsViewController" nib but the view outlet was not set, I thought it worked, clicked on wrong tab – Vikings Jan 27 '12 at 01:32
  • Yes, you'll need to put a view in the nib file, and set it to the `view` outlet of your controller. Then you can add your table view to this view in `viewDidLoad` : `[self.view addSubview:tableView]` – Ell Neal Jan 27 '12 at 01:34
  • Basically, a `UIViewController` **must** have a view by the end of `loadView` (which is where the nib is loaded). – Ell Neal Jan 27 '12 at 01:35
0

You don't show how the cell* variables are getting setup. The message is telling you they're not; that one or more is nil.

smparkes
  • 13,807
  • 4
  • 36
  • 61
  • The cells are set up in interface builder – Vikings Jan 27 '12 at 01:01
  • You still have to load them (I'm pretty sure, but I don't use IB much). My answer still stands, though: you're getting that error because `cellForRowAtIndexPath` is returning `nil`. – smparkes Jan 27 '12 at 01:03
  • I had the same code before when I was creating the table view in interface builder and it received no errors, thanks for the input though – Vikings Jan 27 '12 at 01:04
  • Because then you were loading the NIB that contained the tableview and it contained the cells, too? Now that the tableview isn't in a NIB, presumably the NIB with the cells isn't getting loaded. But, hey, don't believe me that they're `nil`. – smparkes Jan 27 '12 at 01:06
  • I believe you, I was not asking what the error was, I understand the message it is saying. What I do not understand is how to handle this issue. I do not want to create these cells with code. – Vikings Jan 27 '12 at 01:12
  • And yes the nib had the table view, and four cells. – Vikings Jan 27 '12 at 01:13
  • See this question. It seems to go into gory detail about loading views from NIBs and getting them stitched in: http://stackoverflow.com/questions/863321/iphone-how-to-load-a-view-using-a-nib-file-created-with-interface-builder – smparkes Jan 27 '12 at 01:15
  • That looks a little gory. I'm going from memory here, but you should be able to make this work if you use loadNibNamed with the owner as your table view delegate and make sure it has the right cell* IB outlet annotation stuff, then do the connections to files owner in IB. – smparkes Jan 27 '12 at 01:17