StoneBreaker
StoneBreaker

Reputation: 723

Calling loadView for UITableViewController subclass

I make an instance of a subclass of UITableViewController without a xib. I am not going to be using a xib file. I build the data in the -(id)init routine. I create the data for the table in the -(id)init function, and use the methods of the UITableViewDataSource and UITableViewDelegate protocols to display and select the data. I load the UITableViewController subclass into a UINavigationController using the [[UINavigationController alloc] initWithRootViewController: myTVC]; All of this succeeds IF I do not define the loadView method for the class. If I make a blank loadView method an empty UIView is put on the screen.

My Question: How do I write the correct loadView function for a simple subclass of UITableViewController?

Upvotes: 0

Views: 1770

Answers (1)

powerj1984
powerj1984

Reputation: 2226

One should never call through to [super loadView] per Apple's documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/loadView

The correct way to handle this is to simply instantiate a view and set it to self.view, and in this case self.tableView as well:

- (void)loadView {
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;

    self.view = tableView;
    self.tableView = tableView;
}

Upvotes: 3

Related Questions