Trying to construct a tableview with a navigation bar at top

后端 未结 1 1895
南旧
南旧 2021-01-26 07:25

Here\'s the code I used. What am I missing?

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

    CGFloat navBarHeight = 40;

            


        
相关标签:
1条回答
  • 2021-01-26 08:14

    You need to create a containing view in your loadView method and set it as the view on your view controller:

    - (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];
    
    }
    

    Or alternately, if you have a nib file associated with your view controller then you should be using the viewDidLoad method instead of loadView.

    0 讨论(0)
提交回复
热议问题