How to embed a UITableView in a UIScrollview

2019-01-13 19:07发布

问题:

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

I initially have a UIViewController showing a preview section (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

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

EDIT:

Added code for UIScroll view. 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];

回答1:

i have a solution for you,

use only tableView is enough

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

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

you can make it perfectly :)



回答2:

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.

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.



回答3:

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



回答4:

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.



回答5:

Yes,You can use "tableView:viewForHeaderInSection".



回答6:

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.