Customize UITableView header section
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Cycle inside ; building could produce unreliable results Xcode Error
- Dashed line border around UIView
- Date to milliseconds and back to date in Swift
- Datepicker How to popup datepicker when click on edittext
- dealloc in Swift
- Debug certificate expired error in Eclipse Android plugins
- Debug iOS 67 Mobile Safari using the Chrome DevTools
- Debugging with Android Studio stuck at Waiting For Debugger forever
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.