Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a fixed uitableview Header while using sections?

This question should not be mixed up with this here.. These are two different things.

There is a good example how to use a UITableView Header on SO.

This all works fine and the main header is fixed on top as long as the style is set to plain.

But if I use sections, the main header no longer sticks to top and moves away while scrolling to the bottom.

In this method, I am returning the header for each section.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

In this method I am setting the height for the header section above:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

In this method, I am setting the real table header.

- (void)viewWillAppear:(BOOL)animated {     ...     self.recordTableView.tableHeaderView = headerView; } 

Is it even possible having a fixed table header, while using sections? What is an alternative solution to this please?

like image 855
Houman Avatar asked May 31 '14 11:05

Houman


People also ask

How do I scroll the header along with UITableView?

If you have a single header in the table then you can use tableHeaderView as below: tableView. tableHeaderView = Header; Or if you have multiple header in table than you need to use Group table instead of plain table.

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.


2 Answers

If you want a UITableViewController (static cells/keyboard handling) and have a fixed header then you should use Containment. You can do this from a Storyboard by setting up a UIViewController with your fixed header and then using a Container View to embed the UITableViewController.

Storyboard object for Container View

Once you have your containing view setup, you right-click drag from the Container View to the View Controller you want to embed - the UITableViewController in this case.

enter image description here

You can access and get a reference to the contained View Controller (the UITableViewController) from the Container View Controller by implementing the prepareForSegue:sender: method.

like image 73
Joony Avatar answered Oct 16 '22 09:10

Joony


There’s no way to maintain the header of a tableView fixed, but an useful approach when you need a unique header, is to use a UIViewController rather than a UITableViewController, and set the header (UIView) out from the tableView.

Something like this:

enter image description here

like image 41
Matteo Gobbi Avatar answered Oct 16 '22 08:10

Matteo Gobbi