我创建了一个没有xib的UITableViewController子类的实例。我不会使用xib文件。我在-(id)init例程中构建数据。我在-(id)init函数中为表创建数据,并使用UITableViewDataSource和UITableViewDelegate协议的方法来显示和选择数据。我使用[[UINavigationController alloc] initWithRootViewController: myTVC];将UITableViewController子类加载到UINavigationController中,如果我没有为该类定义loadView方法,则所有这一切都将成功。如果我使用空白的loadView方法,则会在屏幕上放置一个空的UIView。

我的问题:如何为UITableViewController的简单子类编写正确的loadView函数?

最佳答案

绝对不要拨打Apple文档中的[super loadView]
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/loadView

处理此问题的正确方法是简单地实例化视图并将其设置为self.view,在这种情况下也将其设置为self.tableView:

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

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

关于iphone - 为UITableViewController子类调用loadView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4370789/

10-13 04:03