How to embed a UITableView in a UIScrollview

2019-01-13 19:06发布

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

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

enter image description here

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

6条回答
Lonely孤独者°
2楼-- · 2019-01-13 19:20

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楼-- · 2019-01-13 19:21

Yes,You can use "tableView:viewForHeaderInSection".

查看更多
放我归山
4楼-- · 2019-01-13 19:24

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 :)

查看更多
爷、活的狠高调
5楼-- · 2019-01-13 19:33

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

查看更多
可以哭但决不认输i
6楼-- · 2019-01-13 19:34

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.

查看更多
地球回转人心会变
7楼-- · 2019-01-13 19:34
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.

查看更多
登录 后发表回答