iOS如何向同一个视图控制器添加静态UITableView和另一个视图

时间:2021-07-22 07:36:54

When I create a static table view and put it in a normal view controller, I get an error which says that static table views must be in UITableViewController's.

当我创建一个静态表格视图并把它放在一个普通的视图控制器中时,我得到一个错误,它说静态表格视图必须在UITableViewController的中。

What I want is a view on the iPad which has a static table view which takes up most of the screen, but a standard UIView under it which always stays visible and has a couple of labels which I update based on content in the table view.

我想要的是iPad上的一个视图它有一个静态的表格视图它占据了大部分的屏幕,但是在它下面有一个标准的UIView它总是可见的并且有一些标签我根据表格视图中的内容更新。

Is there a way to use a UITableViewController and have the tableView not be full screen so that I can add a subview to it in a Storyboard?

有没有一种方法可以使用UITableViewController让tableView不是全屏的这样我就可以在故事板中添加子视图?

If no, can it be done in code?

如果没有,可以用代码完成吗?

Any tips would be appreciated!

任何提示都将被欣赏!

5 个解决方案

#1


0  

The real answer is to create a custom container controller.

真正的答案是创建一个自定义容器控制器。

For instance, you can have a static table view inside a nav controller. So, you need to create a container class, like a nav controller or whatever yours is, and add the static table view controller to that.

例如,您可以在导航控制器中有一个静态表视图。所以,你需要创建一个容器类,比如一个nav控制器或者其他你的,然后添加静态表格视图控制器。

#2


6  

What you need is a regular UITableViewController (so you get all the static cell behavior) and then your view as a subview of the table view, but managed such that it always sticks to the bottom of the screen. Apple demonstrated this as WWDC. Here's what I do (in my example, I have a UIDatePicker that I always want anchored at the bottom):

您需要的是一个常规的UITableViewController(所以您获得了所有的静态单元行为),然后您的视图作为表视图的子视图,但是管理这样的视图始终会一直保持在屏幕的底部。苹果在WWDC上展示了这一点。下面是我所做的(在我的例子中,我有一个我一直想在底部锚定的UIDatePicker):

In viewDidLoad, create your view and add it as a subview of the table view. Place it at the bottom of the table view.

在viewDidLoad中,创建视图并将其作为表视图的子视图添加。将它放在表视图的底部。

[self.tableView addSubview:self.datePicker];
[self updateDatePickerBounds];

Implement scrollViewDidScroll so that you can force your view to the bottom of the screen ever time the table view scrolls:

实现scrollViewDidScroll,以便每当表格视图滚动时,你可以将你的视图强制到屏幕底部:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Keep the date picker floating above the table view, anchored to the bottom of the view
    [self updateDatePickerBounds];
}

And then implement a function that positions your view correctly:

然后实现一个能正确定位视图的函数:

- (void) updateDatePickerBounds
{
    // Keep the date picker floating above the table view, anchored at the bottom
    CGRect tableBounds = self.tableView.bounds;
    CGRect pickerFrame = self.datePicker.frame;
    pickerFrame = CGRectMake(tableBounds.origin.x,
                             tableBounds.origin.y + CGRectGetHeight(tableBounds) - CGRectGetHeight(pickerFrame),
                             tableBounds.size.width,
                             pickerFrame.size.height);
    self.datePicker.frame = pickerFrame;
}

Finally, if you don't want your table view going under your bottom view, just set the table view's contentInset to match the height of your view.

最后,如果不想让表视图位于底部视图之下,只需将表视图的contentInset设置为与视图的高度匹配。

I also call my updateDatePickerBounds method from viewWillAppear. I forget why I had to do that.

我还调用了viewWillAppear中的updateDatePickerBounds方法。我忘了我为什么要那样做。

#3


2  

I am not sure if it works, but you may want to try this approach too:

我不确定它是否有效,但您可能也想试试这种方法:

  1. Create a UIViewController, e.g. BaseViewController (both a class and in Storyboard)
  2. 创建一个UIViewController,例如BaseViewController(包括类和故事板)
  3. Create a UITableViewController e.g. MyStaticTableViewController (only in Storyboard and add an identifier to it)
  4. 创建一个UITableViewController,例如MyStaticTableViewController(仅在故事板中并向其中添加一个标识符)
  5. In Storyboard, add an instance of UIView with appropriate dimensions to your BaseViewcontroller, e.g. myStaticTableView.
  6. 在故事板中,向你的BaseViewcontroller中添加一个具有适当尺寸的UIView实例,例如myStaticTableView。
  7. Programmatically, add the view of MyStaticTableViewController to myStaticTableView.
  8. 以编程方式将MyStaticTableViewController的视图添加到myStaticTableView中。

#4


1  

You can achieve the same effect using UITableViewController and a static UITableView. Create the view you want at the bottom of the table and set the tableFooterView property on UITableView to be that view. This can be done by dragging a UIView object out of the library and adding it to the bottom of the table view (you'll notice the bottom area highlight in blue to know you're doing it correctly). You may have to adjust the content insets to get it to display how you want.

您可以使用UITableViewController和静态UITableView实现相同的效果。在表的底部创建您想要的视图,并将UITableView上的tableFooterView属性设置为该视图。这可以通过从库中拖出UIView对象并将其添加到table视图的底部来完成(您将注意到底部区域用蓝色突出显示,以知道您做得对)。您可能需要调整内容嵌套以使其显示您想要的方式。

#5


1  

Static cell TableViews must be of type UITableViewController, but you can achieve the same effect by using a ViewController with a TableView (or 2) and take care of the cellForRowAtIndexPath method yourself for the TV you want to be "Static Cell". Just switch the IndexPath.section and .row and build the cell the way you want it. More coding sure, but you get the same effect and with more flexibility if you don't want to do it Apple's Static Cell way.

静态单元格表视图必须是UITableViewController类型,但是您可以通过使用带有TableView(或2)的视图控制器来实现相同的效果,并为您想要成为“静态单元格”的电视自己处理cellForRowAtIndexPath方法。只是开关IndexPath。section和.row并按照您希望的方式构建单元格。如果你不想用苹果的静态单元格的方式,你会得到同样的效果和更大的灵活性。

#1


0  

The real answer is to create a custom container controller.

真正的答案是创建一个自定义容器控制器。

For instance, you can have a static table view inside a nav controller. So, you need to create a container class, like a nav controller or whatever yours is, and add the static table view controller to that.

例如,您可以在导航控制器中有一个静态表视图。所以,你需要创建一个容器类,比如一个nav控制器或者其他你的,然后添加静态表格视图控制器。

#2


6  

What you need is a regular UITableViewController (so you get all the static cell behavior) and then your view as a subview of the table view, but managed such that it always sticks to the bottom of the screen. Apple demonstrated this as WWDC. Here's what I do (in my example, I have a UIDatePicker that I always want anchored at the bottom):

您需要的是一个常规的UITableViewController(所以您获得了所有的静态单元行为),然后您的视图作为表视图的子视图,但是管理这样的视图始终会一直保持在屏幕的底部。苹果在WWDC上展示了这一点。下面是我所做的(在我的例子中,我有一个我一直想在底部锚定的UIDatePicker):

In viewDidLoad, create your view and add it as a subview of the table view. Place it at the bottom of the table view.

在viewDidLoad中,创建视图并将其作为表视图的子视图添加。将它放在表视图的底部。

[self.tableView addSubview:self.datePicker];
[self updateDatePickerBounds];

Implement scrollViewDidScroll so that you can force your view to the bottom of the screen ever time the table view scrolls:

实现scrollViewDidScroll,以便每当表格视图滚动时,你可以将你的视图强制到屏幕底部:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Keep the date picker floating above the table view, anchored to the bottom of the view
    [self updateDatePickerBounds];
}

And then implement a function that positions your view correctly:

然后实现一个能正确定位视图的函数:

- (void) updateDatePickerBounds
{
    // Keep the date picker floating above the table view, anchored at the bottom
    CGRect tableBounds = self.tableView.bounds;
    CGRect pickerFrame = self.datePicker.frame;
    pickerFrame = CGRectMake(tableBounds.origin.x,
                             tableBounds.origin.y + CGRectGetHeight(tableBounds) - CGRectGetHeight(pickerFrame),
                             tableBounds.size.width,
                             pickerFrame.size.height);
    self.datePicker.frame = pickerFrame;
}

Finally, if you don't want your table view going under your bottom view, just set the table view's contentInset to match the height of your view.

最后,如果不想让表视图位于底部视图之下,只需将表视图的contentInset设置为与视图的高度匹配。

I also call my updateDatePickerBounds method from viewWillAppear. I forget why I had to do that.

我还调用了viewWillAppear中的updateDatePickerBounds方法。我忘了我为什么要那样做。

#3


2  

I am not sure if it works, but you may want to try this approach too:

我不确定它是否有效,但您可能也想试试这种方法:

  1. Create a UIViewController, e.g. BaseViewController (both a class and in Storyboard)
  2. 创建一个UIViewController,例如BaseViewController(包括类和故事板)
  3. Create a UITableViewController e.g. MyStaticTableViewController (only in Storyboard and add an identifier to it)
  4. 创建一个UITableViewController,例如MyStaticTableViewController(仅在故事板中并向其中添加一个标识符)
  5. In Storyboard, add an instance of UIView with appropriate dimensions to your BaseViewcontroller, e.g. myStaticTableView.
  6. 在故事板中,向你的BaseViewcontroller中添加一个具有适当尺寸的UIView实例,例如myStaticTableView。
  7. Programmatically, add the view of MyStaticTableViewController to myStaticTableView.
  8. 以编程方式将MyStaticTableViewController的视图添加到myStaticTableView中。

#4


1  

You can achieve the same effect using UITableViewController and a static UITableView. Create the view you want at the bottom of the table and set the tableFooterView property on UITableView to be that view. This can be done by dragging a UIView object out of the library and adding it to the bottom of the table view (you'll notice the bottom area highlight in blue to know you're doing it correctly). You may have to adjust the content insets to get it to display how you want.

您可以使用UITableViewController和静态UITableView实现相同的效果。在表的底部创建您想要的视图,并将UITableView上的tableFooterView属性设置为该视图。这可以通过从库中拖出UIView对象并将其添加到table视图的底部来完成(您将注意到底部区域用蓝色突出显示,以知道您做得对)。您可能需要调整内容嵌套以使其显示您想要的方式。

#5


1  

Static cell TableViews must be of type UITableViewController, but you can achieve the same effect by using a ViewController with a TableView (or 2) and take care of the cellForRowAtIndexPath method yourself for the TV you want to be "Static Cell". Just switch the IndexPath.section and .row and build the cell the way you want it. More coding sure, but you get the same effect and with more flexibility if you don't want to do it Apple's Static Cell way.

静态单元格表视图必须是UITableViewController类型,但是您可以通过使用带有TableView(或2)的视图控制器来实现相同的效果,并为您想要成为“静态单元格”的电视自己处理cellForRowAtIndexPath方法。只是开关IndexPath。section和.row并按照您希望的方式构建单元格。如果你不想用苹果的静态单元格的方式,你会得到同样的效果和更大的灵活性。