在导航tableview时添加到superview的视图消失了

时间:2013-04-17 21:08:41

标签: ios objective-c tableview superview

我有一个tableView,当用户选择一个tableView单元格时,它会导航到详细视图。这个主TableView是在UITableViewController类中创建的。 (MasterViewController)

我使用StoryBoard创建主 - 详细信息表视图。

在MasterViewController中,当用户选择To按钮时,我在主tableView的顶部放置一个tableView。第二个tableView允许用户从该列表中选择多个值。

为了防止第一个(主)tableView滚动时第二个tableView在屏幕上滚动,我将第二个tableView添加到MasterViewController视图的 superView 。 ([self.view.superview addSubview:_toView];)

因此,在MasterViewController中,我添加了一个TableViewController(_toController)。我实例化一个UIView(_toView),向它添加一个背景图像(_toViewBG),然后向它添加一个TableView(_toTableView)。然后我将_toView(包含第二个tableView)添加到MasterViewController视图的superview中。

_toController = [[ToTableViewController alloc] init];
_toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,263,247)];

_toViewBG = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,257,232)];
_toViewBG.image = [UIImage imageNamed:@"toViewBackground.png"];

[_toView addSubview:_toViewBG];

_toTableView = [[UITableView alloc] initWithFrame:CGRectMake(20,30,220,180) style:UITableViewStylePlain];

[_toTableView setDelegate:_toController];
[_toTableView setDataSource:_toController];
[_toView addSubview:_toTableView];

[self.view.superview addSubview:_toView];
[_toTableView reloadData];

当我滚动主tableView时,这种方法使第二个tableView不滚动。当主tableView滚动时,第二个tableView保持在顶部并就位。

如果我在主TableView中选择一个单元格,我会导航到详细视图。当我返回主TableView时,我无法显示第二个TableView(_toView)。

我不知道如何告诉_toView来到前面。我意识到它被添加到MasterViewController视图的superview中。从一些实验来看,这个超级视图似乎是一个CALayer。

有人可以建议我如何让这个_toView显示在MasterViewController视图的前面吗?

提前感谢您的任何帮助。

解决 我最后只是将第二个tableview添加为MasterViewController视图的子视图。     [self.view addSubview:_toView]; 然后我在主桌面视图上禁用滚动,同时我的第二个桌面视图可见。

Self.tableView.scrollEnabled  = No;

似乎工作正常。

1 个答案:

答案 0 :(得分:1)

可以通过调用[self.view.superview bringSubviewToFront:_toView]来完成这项工作,但我的建议是让MasterViewController成为UIViewController的子类,而不是UITableViewController,然后添加您的UITableView个对象都是根视图的子视图(而不是引用superview)。

相关问题