ios 我可以在滚动时固定表格的 tableHeaderView 位置吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7433681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:32:53  来源:igfitidea点击:

Can i make a table's tableHeaderView position fixed as I scroll?

iphoneobjective-ciosuitableview

提问by Alex

I have a table view and have attached to its tableHeaderView a UISegmentedControl. How can I make the tableHeaderView fixed so that i can always view the UISegmentedControl in the same position even when i am scrolling the table view?

我有一个表格视图,并在其 tableHeaderView 上附加了一个 UISegmentedControl。如何使 tableHeaderView 固定,以便即使在滚动表格视图时也可以始终在同一位置查看 UISegmentedControl?

采纳答案by Serhii Mamontov

tableView:ViewForHeaderInSection:is your option to achieve your task. In plain table this will somehow looks like Address Book app with first char of name in section, but you will have your segmented control

tableView:ViewForHeaderInSection:是您完成任务的选择。在普通表格中,这将在某种程度上看起来像地址簿应用程序,在部分中具有名称的第一个字符,但您将拥有分段控制

回答by mskw

First lower the tableview's frame(make space on top of tableview). Then during scrollViewDidscroll, fix the tableviewheader, properly, so it doesn't scroll with it.

首先降低tableview的框架(在tableview顶部留出空间)。然后在 scrollViewDidscroll 期间,正确修复 tableviewheader,使其不会随之滚动。

Thats the summary of it.

这就是它的总结。

回答by Steve Moser

To expand on @mskw's answer:

要扩展@mskw 的答案:

You can use the UITableViewController (keep the niceties such as UIRefreshControl support and keyboard avoidance). You just have to embed your toolbar in a plain view and place that in your tableHeaderView. Then implement this scroll view delegate method to lock.

您可以使用 UITableViewController(保留 UIRefreshControl 支持和键盘避免等细节)。您只需将您的工具栏嵌入到一个普通视图中并将其放置在您的 tableHeaderView 中。然后实现这个滚动视图委托方法来锁定。

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect rect = self.toolbarContainerView.frame;
    rect.origin.y = MIN(0,scrollView.contentOffset.y + scrollView.contentInset.top);
    self.toolbarContainerView.frame = rect;
}

Note that if you also use section headers you will have to send those views behind your tableHeaderView otherwise they will float over the tableHeaderView.

请注意,如果您还使用部分标题,则必须将这些视图发送到 tableHeaderView 后面,否则它们将漂浮在 tableHeaderView 上。

回答by Mark Armstrong

I would suggest placing the UISegmentedControl in a separate view on top of the UITableView rather than in the tableHeaderView. You might also want to set yourTable.bounces = NO;in order to keep the header view from bouncing when you get to the top of the table.

我建议将 UISegmentedControl 放在 UITableView 顶部的单独视图中,而不是放在 tableHeaderView 中。您可能还想设置yourTable.bounces = NO;以防止标题视图在到达表格顶部时弹跳。