Customize UITableView header section
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Customizing a UITableView section header is straightforward once you decide how much control you need. For a plain label, the built-in header title works. For custom fonts, spacing, colors, buttons, or reusable layouts, return a custom view or a UITableViewHeaderFooterView subclass.
Start with the Simple Built-In Header
If the only requirement is a title, use tableView(_:titleForHeaderInSection:). It is the lightest option and works well for basic grouped lists.
As soon as design requirements go beyond plain text, move to a custom header. The default title API gives very limited styling control.
Return a Custom Header View
For more control, implement tableView(_:viewForHeaderInSection:) and provide an explicit height.
This approach is fine for simple custom designs, but it can get repetitive if several sections share the same header layout.
Use UITableViewHeaderFooterView for Reuse
For more complex headers, subclass UITableViewHeaderFooterView. This gives you reuse behavior similar to table view cells and keeps header code out of the controller.
Register and dequeue it like this:
This is the better design when the header includes accessory buttons, dynamic state, or reusable styling.
Common Pitfalls
The most common mistake is using titleForHeaderInSection and expecting full control over fonts, spacing, or background styling. That API is intentionally limited.
Another common issue is returning a custom header view but forgetting to supply a matching height. If the table view gives the header too little space, the layout may appear clipped or broken.
Reuse is another source of bugs. If a reusable header contains buttons or stateful subviews, reset any old state when configuring it for a new section. Otherwise tap handlers, hidden flags, or old titles can leak across sections.
Finally, set colors on the correct view. UITableViewHeaderFooterView has both the header view itself and its contentView, and styling the wrong one can produce confusing results.
Summary
- Use
titleForHeaderInSectionfor plain text-only headers. - Use
viewForHeaderInSectionwhen you need custom layout or styling. - Set an explicit header height that matches the layout.
- Prefer
UITableViewHeaderFooterViewfor reusable or interactive section headers. - Reset reused header state carefully so one section does not inherit another section's UI.

