简体   繁体   English

如何在UIScrollview中嵌入UITableView

[英]How to embed a UITableView in a UIScrollview

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];

在此输入图像描述

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 ie 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中的内容仍然是可滚动的

在此输入图像描述

EDIT: 编辑:

Added code for UIScroll view. 添加了UIScroll视图的代码。 However, nothing changed for me. 但是,对我来说没有任何改变。 What is wrong with my code? 我的代码出了什么问题?

//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];

i have a solution for you, 我有一个解决方案,

use only tableView is enough 仅使用tableView就足够了

  1. make the "Preview View" as the HeaderView of your tableView 将“预览视图”设置为tableView的HeaderView

  2. make the "Search Bar" as the Section Header View of your tableView 将“搜索栏”设为tableView的Section Header视图

you can make it perfectly :) 你可以做到完美:)

No need for anything fancy or custom, use a UITableView and set your preview pane to be the tableHeaderView and 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个部分,那么部分标题将一直存在。

Or you can just do these steps: 1. Disable scrolling of the tableView. 或者您可以执行以下步骤:1。禁用滚动tableView。 2. Set height constraint to the tableView and a IBOutlet connected to it. 2.将高度约束设置为tableView和连接到它的IBOutlet。 3. Before you reload the data, calculate the table height. 3.在重新加载数据之前,计算表格高度。 heightConstraint.constant = (heightOfTheSingleTableViewCell) * numOfRows

You will have to embed your UITableView inside of a UIScrollView that has the size of the screen minus the navigation bar. 您必须在UIScrollView中嵌入UITableView,其大小与屏幕一样减去导航栏。 Set the UIScrollViews background to clearColor to allow your content above the UITableView to remain visible. 将UIScrollViews背景设置为clearColor,以允许UITableView上方的内容保持可见。

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

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

It can not scroll because contentSize is not large enough. 它无法滚动,因为contentSize不够大。 You need to increase the length to total length of your content that are on top of the scrollView. 您需要将内容的长度增加到scrollView顶部的内容的总长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM