Fixed Header to Uitableview

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

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:

Sample Image

UITableView with fixed section headers

The headers only remain fixed (floating) when the UITableViewStyle property of the table is set to UITableViewStylePlain.

If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells (will not float).

UITableview: Fixed Section Headers

You should add separate tableViews instead of sections in same tableView. That way your tableView's frame will be fixed and adding row to either won't push others frame down.

Avoid sticky header view when tableview scrolls with Auto Layout

Currently your table view and your label are siblings inside your UIViewController's view, which means your label is not part of the table view so it won't scroll with it. You can add the label to a UIView, set it's constraints and then set the tableHeaderView property of the table view. Here's a sample code with some hardcoded values:

    let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "some text"
label.sizeToFit()

let headerView = UIView()
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.addSubview(label)

tableView.tableHeaderView = headerView

headerView.centerXAnchor.constraint(equalTo: tableView.centerXAnchor).isActive = true
headerView.heightAnchor.constraint(equalToConstant: 80).isActive = true
headerView.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true

label.leftAnchor.constraint(equalTo: headerView.leftAnchor, constant: 50).isActive = true

tableView.tableHeaderView?.layoutIfNeeded()

Fixed header to UITableview?

I finally figured this out right after posting. Figures. :)

Here's what I did, in case others run into the same problem:

  1. Delete the existing UITableViewController and its XIB. They're junk. Get really mad while you do.

  2. Make a new UIViewController subclass with a XIB

  3. Open XIB in IB and add your header stuff and a UITableView to the UIView

  4. In the IB Outlets for UITableView make sure you connect Delegate and DataSource to your File Owner

  5. In the header for your view controller, be sure to add <UITableViewDelegate, UITableViewDataSource> to implement these protocols

  6. Implement all the regular UITableView delegate and data source methods you know and love, but in your UIViewController instead of the way you're used to doing it through UITableViewController

After this things should work.

How to make a sticky footer and header with a tableview?

Don't confuse the section header and footer with the table header and footer.

  • The section header and footer, in a non-grouped table, are pinned to the top and bottom of the table while you scroll.

  • But the table header and footer, as you rightly say, are sort of like cells: they are before the first section and after the last section, and they scroll with the table.

If that's not what you want — that is, if there is something you want to show above the table and below the table, all the time — then you need to put them above and below the table view as completely separate views:

thing above
table
thing below

Of course, if you do that, you can't use a UITableViewController. You'll have to have a normal view controller, with the table view as an embedded view and the table view controller as a child view controller.



Related Topics



Leave a reply



Submit