ios 如何在 UIScrollview 中嵌入 UITableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10508524/
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 18:17:23  来源:igfitidea点击:

How to embed a UITableView in a UIScrollview

iosuitableviewuiviewcontrolleruiscrollviewscroll

提问by Zhen

How can I do the following in a UIViewController (containing a tableview as a subview)

如何在 UIViewController 中执行以下操作(包含 tableview 作为子视图)

I initially have a UIViewController showing a preview section (UIView)

我最初有一个 UIViewController 显示预览部分(UIView)

//Setup container for preview section
UIView *tempContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 100)];
self.preview_answer_container = tempContainer;
self.preview_answer_container.backgroundColor = [UIColor clearColor];
[tempContainer release];
[self.view addSubview:self.preview_answer_container];

I also added a UITableView (and a searchbar) below

我还在下面添加了一个 UITableView(和一个搜索栏)

//setup tableview
UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,44 ,320,self.view.frame.size.height - 44)];
self.tagFriendsTableView = tempTable;
self.tagFriendsTableView.delegate = self;
self.tagFriendsTableView.dataSource = self;
[tempTable release];

[self.view addSubview:self.tagFriendsTableView];

enter image description here

在此处输入图片说明

With this setup, my scrolling area is small (only static area below preview section)

使用此设置,我的滚动区域很小(只有预览部分下方的静态区域)

How can I modify my code such that 1) I can scroll the whole page upwards i.e. preview section and tableview will scroll up together 2) Scrolling as a whole will stop when the search bar reaches the top (below navbar) (see screenshot), but the contents in the UITableView is still scrollable

如何修改我的代码,以便 1)我可以向上滚动整个页面,即预览部分和 tableview 将一起向上滚动 2)当搜索栏到达顶部(导航栏下方)时,整个滚动将停止(见截图),但 UITableView 中的内容仍然是可滚动的

enter image description here

在此处输入图片说明

EDIT:

编辑:

Added code for UIScroll view. However, nothing changed for me. What is wrong with my code?

添加了 UIScroll 视图的代码。然而,对我来说什么都没有改变。我的代码有什么问题?

//setup scroll view
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

//Search
UISearchBar *tempBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 120 + 20, 320, 44)];
self.sBar = tempBar;
[tempBar release];
self.sBar.delegate = self;
self.sBar.tintColor = [UIColor colorWithHexString:@"#b6c0c7"];
self.sBar.placeholder = @"Search for FB and DM friends";

[scroll addSubview:sBar];

//setup tableview
UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,144 + 20 + 20 ,320,self.view.frame.size.height - 144 - 20 - 20 - 44)];
self.tagFriendsTableView = tempTable;
self.tagFriendsTableView.delegate = self;
self.tagFriendsTableView.dataSource = self;
[tempTable release];

//Add the scroll view to the parent view
[scroll addSubview:self.tagFriendsTableView];

scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];

回答by adali

i have a solution for you,

我有一个解决方案给你

use only tableView is enough

只使用 tableView 就足够了

  1. make the "Preview View" as the HeaderView of your tableView

  2. make the "Search Bar" as the Section Header View of your tableView

  1. 将“预览视图”作为 tableView 的 HeaderView

  2. 将“搜索栏”作为 tableView 的部分标题视图

you can make it perfectly :)

你可以做到完美:)

回答by DBD

No need for anything fancy or custom, use a UITableViewand set your preview pane to be the tableHeaderViewand your search field to be the section header using tableView:viewForHeaderInSection:in your UITableViewDelegate.

无需任何幻想或定制,使用UITableView和设置预览窗格是tableHeaderView与你的搜索字段是使用节头tableView:viewForHeaderInSection:在你的UITableViewDelegate

The table header will scroll off the top during scroll event. Section headers float and stay visible the whole time that section if visible. If you only have 1 section, then the section header will be there the whole time.

在滚动事件期间,表格标题将从顶部滚动。如果可见,部分标题会浮动并在该部分的整个时间内保持可见。如果您只有 1 个部分,则部分标题将一直存在。

回答by Slavcho

Or you can just do these steps: 1. Disable scrolling of the tableView. 2. Set height constraint to the tableView and a IBOutlet connected to it. 3. Before you reload the data, calculate the table height. heightConstraint.constant = (heightOfTheSingleTableViewCell) * numOfRows

或者您可以只执行以下步骤: 1. 禁用 tableView 的滚动。2. 为 tableView 和连接到它的 IBOutlet 设置高度约束。3. 在重新加载数据之前,计算表格高度。heightConstraint.constant = (heightOfTheSingleTableViewCell) * numOfRows

回答by rhummelmose

You will have to embed your UITableView inside of a UIScrollView that has the size of the screen minus the navigation bar. Set the UIScrollViews background to clearColor to allow your content above the UITableView to remain visible.

您必须将 UITableView 嵌入到 UIScrollView 中,该 UIScrollView 具有屏幕大小减去导航栏。将 UIScrollViews 背景设置为 clearColor 以允许 UITableView 上方的内容保持可见。

回答by zucknet

Yes,You can use "tableView:viewForHeaderInSection".

是的,您可以使用“tableView:viewForHeaderInSection”。

回答by Ohmy

scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

It can not scroll because contentSize is not large enough. You need to increase the length to total length of your content that are on top of the scrollView.

它无法滚动,因为 contentSize 不够大。您需要将长度增加到滚动视图顶部的内容的总长度。