尝试在顶部构建一个带有导航栏的tableview [英] Trying to construct a tableview with a navigation bar at top
本文介绍了尝试在顶部构建一个带有导航栏的tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我使用的代码。我缺少什么?
Here's the code I used. What am I missing?
- (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];
}
而不是下面有桌子的导航栏,我得到一个黑屏状态栏。
Instead of a nav bar with a table underneath, I get a black screen under the status bar.
推荐答案
您需要在loadView方法中创建一个包含视图,并将其设置为视图控制器上的视图:
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];
}
或者,如果您的nib文件与您的关联查看控制器然后你应该使用viewDidLoad方法而不是loadView。
Or alternately, if you have a nib file associated with your view controller then you should be using the viewDidLoad method instead of loadView.
这篇关于尝试在顶部构建一个带有导航栏的tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文