将多个视图添加到容器视图

时间:2015-03-03 23:16:42

标签: ios objective-c uitableview uiviewcontroller uicontainerview

我正在考虑向现有的UI视图控制器添加大量的TableView。

我见过https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html这似乎是我想去的方式。

然而,我的问题是我可以将多个TableView嵌入到单个ContainerView中吗?

我的应用程序会进行各种测试,并且会有结果。每次测试完成后,我想添加一个TableView,使它们整齐地出现在彼此之下。

这可能吗?

2 个答案:

答案 0 :(得分:1)

是的,你可以。你可以做两件事:

  

ONE View Controller - 多个表视图(作为根视图的子视图)

  

一个容器控制器 - 许多使用VC包含的ChildViewControllers(并且每个都有一个表视图)

答案 1 :(得分:0)

这是你可以在ViewDidLoad中为UIViewController子类做的。

// Do any additional setup after loading the view, typically from a nib.

CGRect frame = self.view.frame;

// this container shall hold the two tables that I am going to add later
// container shall share the same size as view controller's "view"
UIView *container = [[UIView alloc] initWithFrame:frame];

frame.size.height = frame.size.height / 2; // I want to fit two table view vertically to cover the container view

UITableView *tableView1 = [[UITableView alloc] initWithFrame:frame];
tableView1.backgroundColor = [UIColor redColor];
// ...
// ...
// Assign more table view properties here
// ...
// ...
[container addSubview:tableView1];

frame.origin.y = frame.size.height; // update coordinates for the second table view

UITableView *tableView2 = [[UITableView alloc] initWithFrame:frame];
tableView2.backgroundColor = [UIColor greenColor];
// ...
// ...
// Assign more table view properties here
// ...
// ...
[container addSubview:tableView2];

[self.view addSubview:container];

您必须为所有tableviews设置Delegates才能使它们正常工作,但这应该会让您有所收获。 (我添加了一些不同的背景颜色来区分两种表格视图)

相关问题