Adding iOS UITableView HeaderView not section header
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UITableView supports two different header concepts, and mixing them up causes a lot of confusion. A table header view sits once at the top of the entire table, while a section header belongs to a specific section and is managed through delegate methods.
Table Header View Versus Section Header
If you want one banner, profile block, search area, or summary card above all rows, use tableHeaderView. Do not implement tableView(_:viewForHeaderInSection:) unless the header belongs to a particular section.
The table-level header is attached directly to the table:
That is the entire API surface, which is why the feature looks deceptively simple. Most of the real work is sizing the header correctly.
Add a Simple Header Programmatically
For a fixed-height header, you can create a view, assign a frame, and attach it.
That is enough for simple static headers. Unlike reusable section headers, a table header is just an ordinary view instance.
Auto Layout Needs an Extra Step
The most common surprise is that tableHeaderView does not automatically resize itself from constraints the same way cells do. If the header uses Auto Layout, you usually need to calculate the fitted height, update the frame, and assign the header back to the table.
Reassigning tableView.tableHeaderView = header after changing the frame is important. Without that, the table often does not pick up the new height.
Loading a Header from a XIB
If the header is substantial, a xib-backed view is often cleaner than building it inline.
Then load and attach it:
You still need proper sizing logic if the height is content-driven.
When a Section Header Is the Better Tool
Use a section header when the content repeats per section or should scroll and pin according to section behavior. Use tableHeaderView when the header is a single top-level decoration or summary for the entire table.
That distinction matters because table headers are not reused and are not part of the section-delegate lifecycle.
Common Pitfalls
The biggest mistake is implementing a section header when the UI actually needs a table header. That creates repeated or misplaced views.
Another common issue is expecting Auto Layout alone to size tableHeaderView. Unlike cells, table headers usually need explicit frame adjustment.
Developers also change the header's height but forget to assign it back to tableView.tableHeaderView. The frame changes in memory, but the table does not refresh its layout.
Finally, do not assume a table header is reusable like UITableViewHeaderFooterView. It is a single attached view, so manage it like a normal view object.
Summary
- Use
tableView.tableHeaderViewfor one header above the entire table. - Use section headers only for per-section content.
- Fixed-size headers are simple; Auto Layout headers need explicit height calculation.
- After resizing a table header, assign it back to
tableView.tableHeaderView. - Table headers are ordinary views, not reusable section header objects.

