[英]Calling loadView for UITableViewController subclass
我創建了一個沒有xib的UITableViewController子類的實例。 我將不會使用xib文件。 我在-(id)init
例程中構建數據。 我在-(id)init
函數中為表創建數據,並使用UITableViewDataSource和UITableViewDelegate協議的方法來顯示和選擇數據。 我使用[[UINavigationController alloc] initWithRootViewController: myTVC];
將UITableViewController子類加載到UINavigationController中[[UINavigationController alloc] initWithRootViewController: myTVC];
如果我沒有為該類定義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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.