ios - 试图构建一个顶部带有导航栏的表格 View

标签 ios uitableview uinavigationbar

这是我使用的代码。我错过了什么?

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

我在状态栏下方看到一个黑屏,而不是下方有表格的导航栏。

最佳答案

您需要在 loadView 方法中创建一个包含 View ,并将其设置为 View Controller 上的 View :

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

}

或者,如果您有一个与 View Controller 关联的 nib 文件,那么您应该使用 viewDidLoad 方法而不是 loadView。

关于ios - 试图构建一个顶部带有导航栏的表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6990915/

相关文章:

html - cordova/phonegap 将 body div 调整为 ios 中的屏幕 - xcode

ios - 现在 Swift 是开源的,我可以在不是 Mac 的计算机上编写和编译 iOS 应用程序吗?

ios - 如何修复 iPhone 后退按钮图像位置?

ios - 优化向 UIStackView 添加 View

ios - UITableView 延迟 reloadData 直到动画完成

ios - dequeueReusableCellWithIdentifier 中的副标题

ios - 我怎样才能让 tableViewController 知道 managedObjectContext 在外面发生了变化?

MKMapView使导航栏变得透明

ios - 创建一个 UINavigationController 目前 2 UIViewController 在 Objective-C 中

ios - RestKit:当值无效时如何拒绝整个映射