gpt4 book ai didi

objective-c - iOS 如何将 STATIC UITableView 和另一个 View 添加到同一个 View Controller

转载 作者:可可西里 更新时间:2023-11-01 04:22:58 24 4
gpt4 key购买 nike

当我创建一个静态 TableView 并将其放入普通 View Controller 中时,我收到一条错误消息,指出静态 TableView 必须位于 UITableViewController 中。

我想要的是 iPad 上的一个 View ,它有一个占据大部分屏幕的静态表格 View ,但它下面有一个标准的 UIView,它始终保持可见,并且有几个标签,我根据表格 View 。

有没有办法使用 UITableViewController 并让 tableView 不是全屏,以便我可以在 Storyboard中向其添加 subview ?

如果不是,是否可以用代码完成?

如有任何提示,我们将不胜感激!

最佳答案

您需要的是一个常规的 UITableViewController(这样您就可以获得所有静态单元格行为),然后将您的 View 作为 TableView 的 subview ,但对其进行管理以使其始终位于底部屏幕。 Apple 在 WWDC 上展示了这一点。这是我所做的(在我的示例中,我有一个 UIDatePicker,我总是希望将其固定在底部):

viewDidLoad 中,创建您的 View 并将其添加为 TableView 的 subview 。将它放在表格 View 的底部。

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

实现scrollViewDidScroll,这样每当表格 View 滚动时,您都可以强制将 View 置于屏幕底部:

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

然后实现一个正确定位 View 的函数:

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

最后,如果您不希望您的表格 View 位于底部 View 下方,只需将表格 View 的 contentInset 设置为与您的 View 高度相匹配。

我还从 viewWillAppear 调用了我的 updateDatePickerBounds 方法。我忘了为什么我必须这样做。

关于objective-c - iOS 如何将 STATIC UITableView 和另一个 View 添加到同一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8763462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com