2011-08-09 60 views
0

以下是我使用的代碼。我錯過了什麼?嘗試在頂部使用導航欄構建表格視圖

- (void)loadView 
{ 
    CGSize screen_size = [[UIScreen mainScreen] bounds].size; 

    CGFloat navBarHeight = 40; 

    UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)]; 

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain]; 

    table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    table.delegate = self; 
    table.dataSource = self; 
    table.editing = YES; 
    [table reloadData]; 

    [self.view addSubview:nav]; 
    [self.view addSubview:table]; 
    [nav release]; 
    [table release]; 
} 

取而代之的是一個帶有下方表格的導航欄,我在狀態欄下會看到一個黑屏。

回答

2

你需要在你的loadView方法來創建一個包含視圖,並將其設爲您的視圖控制器上的觀點:

- (void)loadView { 


    CGSize screen_size = [[UIScreen mainScreen] bounds].size; 

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screen_size.width,screen_size.height)]; 
    self.view = myView; 

    CGFloat navBarHeight = 40; 

    UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)]; 

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain]; 

    table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    table.delegate = self; 
    table.dataSource = self; 
    table.editing = YES; 
    [table reloadData]; 

    [self.view addSubview:nav]; 
    [self.view addSubview:table]; 
    [nav release]; 
    [table release]; 
    [myView release]; 

} 

或者交替,如果您有您的視圖控制器,然後相關的,你應該筆尖文件使用viewDidLoad方法代替loadView。