简体   繁体   中英

How to embed a UITableView in a UIScrollview

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

在此输入图像描述

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

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

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.

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

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.

是的,您可以使用“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. You need to increase the length to total length of your content that are on top of the scrollView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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