简体   繁体   English

UINavigationController中的UITableView

[英]UITableView inside UINavigationController

Im creating a UITableView inside my UINavigationController, and the problem is that the table is covering the header of it. 我在我的UINavigationController内部创建了一个UITableView,问题是表格覆盖了它的标题。 This is my class: 这是我的课:

.h: 。H:

@interface SecondViewController : UINavigationController

@end

And part of the .m: 和.m的一部分:

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UITableView *mmm = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,320, 100) style:UITableViewStylePlain];

[self.view addsubview:mmm];


}

What am I doing wrong? 我究竟做错了什么?

I don't think this is what you want to do. 我不认为这是您想要做的。

The documentation for UINavigationController advises against subclassing. UINavigationController的文档建议不要使用子类。 Instead, I think you want to create a UITableViewController and push that controller onto the navigation controller's stack (or if this is root view controller, then initialize a navigation controller with that UITableViewController . 相反,我认为您想创建一个UITableViewController并将该控制器推入导航控制器的堆栈中(或者如果它是根视图控制器,则使用该UITableViewController初始化一个导航控制器。

MyTableViewController *tableController = [[MyTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableController];
//  now add the navController's view to the view hierarchy, however you intend to do that.

The frame of your UITableView will start at origin point 0.0 , 0.0 witch is the origin point of your UINavigationController. UITableView的框架将从起点0.0开始,0.0是UINavigationController的起点。 Just change the 只需更改

CGRectMake(0, 0,320, 100)

to

CGRectMake(0, 44,320, 100)

FYI it is not recommended to subclass a UINavigationCantroller. 仅供参考,不建议将UINavigationCantroller子类化。

You are creating your frame for the UITableView with a top-left point of 0,0 on the screen. 您正在为UITableView创建框架,其屏幕的左上角为0,0。 This pulls your table up as high as can be allowed. 这将您的桌子拉得尽可能高。 You can do one of two things to correct this: 您可以执行以下两项操作之一来更正此问题:

1) Build the View in IB and wire it to your controller. 1)在IB中构建视图并将其连接到控制器。 IN IB you can tell the layout manager you have different controls displayed such as the status bar, navigation controller or tab bar. 在IB中,您可以告诉布局管理器您显示了不同的控件,例如状态栏,导航控制器或选项卡栏。 Having these in place will allow you to precisely place the UITableView. 将它们放在适当的位置将使您可以精确地放置UITableView。

2) Alter the Rect used to place your table view on the page. 2)更改用于在页面上放置表格视图的Rect。 You will need to add to the second argument to move the frame down. 您将需要添加到第二个参数来向下移动框架。 You can play with how much you add until the table view is where you want it. 您可以播放添加的数量,直到表格视图位于所需位置为止。 This number represents pixels and the higher the number, the lower on the screen the top-left corner of the control will be placed. 该数字表示像素,数字越大,控件左上角的位置在屏幕上的位置就越低。

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM