iphoneuitableviewloadview

overriding loadView in a UITableViewController subclass


I am trying to setup a UITableViewController so that its tableView belongs to my custom subclass. My loadView method currently looks like this:

- (void) loadView {
   MyTableViewSubclass* tv = [[[MyTableViewSubclass alloc]initWithFrame: CGRectZero style: UITableViewStylePlain]autorelease];
   self.view = tv;
   self.tableView = tv;
}

I am getting crashes later on that go away if I comment out the above method. So something is missing. But what?

Apple's documentation says I should not be calling super in loadView. Which makes sense, because I want the view to have my class, not theirs.

Things I've tried that don't help:

One thing I've tried that does fix the problem, but defeats the purpose:

EDIT: the crash is shown below. It happens after the user does some input. It also happens the same way if I am creating a plain UITableView instead of my subclass. There is a lot going on in the app, and something in my loadView override [or more likely, something missing from my override] is causing the state to be different, which in turn leads to the crash. But I don't see a good way to track what is different.

2011-09-08 12:44:59.591 MyAppName[97649:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[MyTableViewSubclass scrollToRowAtIndexPath:atScrollPosition:animated:]: row (0) beyond bounds (0) for section (0).'

Solution

  • Turns out I need to set the dataSource and delegate of my tableView as a part of loading. So when I do this, everything works fine:

    - (void) loadView {
       MyTableViewSubclass* tv = [[[MyTableViewSubclass alloc]initWithFrame: CGRectZero style: UITableViewStylePlain]autorelease];
       tv.dataSource = self;
       tv.delegate = self;
       self.view = tv;
       self.tableView = tv;
    }