17

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];
Zhen
  • 12,361
  • 38
  • 122
  • 199

6 Answers6

33

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

adali
  • 5,977
  • 2
  • 33
  • 40
4

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.

DBD
  • 23,075
  • 12
  • 60
  • 84
2

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

Slavcho
  • 2,792
  • 3
  • 30
  • 48
1

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.

rhummelmose
  • 647
  • 4
  • 15
  • Hi, thanks for your response. I added some code for UIScroll view but it didn't work for me i.e. nothing changed from my original implementation. Can you help me have a look at my edited question? I added my uiscrollview code there. Thanks in advance – Zhen May 09 '12 at 01:34
  • You should actually go with DBD/adali's solution, it's better than mine :) – rhummelmose May 09 '12 at 07:29
1

Yes,You can use "tableView:viewForHeaderInSection".

zucknet
  • 41
  • 6
0
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.

Ohmy
  • 2,201
  • 21
  • 24