UITableView hide header from empty section
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Hiding a UITableView header for an empty section is slightly trickier than returning an empty string. UIKit can still reserve header height or display spacing unless the title, custom view, and height logic all agree that the section should be hidden.
Keep Section Visibility in One Rule
The safest pattern is to define one helper that decides whether a section should be visible:
Once that rule exists, reuse it everywhere header behavior is decided. Without a shared rule, it is easy to hide the title but still leave a visible blank gap because the height method returns a nonzero value.
Return No Header Content for Empty Sections
If you use the default text header, return nil when the section has no rows:
That removes the text, but it is not always enough to remove the space. Many table styles still reserve a default height unless you handle that separately.
Return a Minimal Height for Empty Sections
To fully hide the header, override the height delegate and return .leastNormalMagnitude for empty sections:
Using 0 does not always behave as expected. In grouped or inset-grouped tables, UIKit may still apply default spacing. .leastNormalMagnitude is the common workaround when you want the header effectively removed.
Match Custom Header Views to the Same Rule
If you provide a custom header view, it must follow the same condition:
If viewForHeaderInSection creates a view while heightForHeaderInSection tries to hide it, you end up with inconsistent results and harder-to-debug layout artifacts.
Reload Sections When Data Changes
Header visibility often breaks after dynamic updates because the table view still uses stale layout state. When rows change, reload the affected section:
That keeps the row count and header state synchronized. If you update the model but do not reload the section, the table may continue drawing the old header configuration.
Consider Filtering Empty Sections Out
If empty sections never need to appear in the interface, a cleaner approach is to remove them before presenting the data:
This simplifies the delegate methods because the table view no longer needs special empty-section behavior. The tradeoff is that the visible section indices may no longer match the original data source indices.
Common Pitfalls
- Returning
nilfor the header title but forgetting to reduce the header height for empty sections. - Returning
0height and expecting all table styles to remove spacing completely. - Using one condition in
titleForHeaderInSectionand a different condition inviewForHeaderInSection. - Updating the data model without reloading the affected section.
- Keeping empty sections in the table when the simpler design would be to filter them out.
Summary
- Hide empty-section headers by coordinating title, custom view, and height logic.
- Use one shared helper so every header decision follows the same visibility rule.
- Return
.leastNormalMagnituderather than0when you want empty headers truly hidden. - Reload sections after data changes so header state stays in sync.
- If the UX allows it, remove empty sections from the data before rendering.
Related reading
- UITableview How to Disable Selection for Some Rows but Not Others
- UITableView load more when scrolling to bottom like Facebook application
- UITableView not showing first time, only when scrolling a bit
- UITableview reloaddata with animation
- UITableView row animation duration and completion callback
- UITableView, Separator color where to set?
- UITableView set to static cells. Is it possible to hide some of the cells programmatically?
- UITableView viewForHeaderInSection not called during reloadData
.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.