Space Between Sections in UITableView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The space between UITableView sections is usually controlled by section headers, section footers, and table style defaults rather than by a single "section spacing" property. To get predictable results, you need to decide whether the gap should come from header height, footer height, custom views, or the newer top-padding behavior in iOS.
Use Header and Footer Heights Deliberately
In a table view, the visible gap between one section and the next usually comes from:
- the footer of the previous section
- the header of the next section
- style-specific default padding in
plain,grouped, orinsetGroupedtables
You can control those values through delegate methods:
This adds spacing above and below each section. If you want only one visible gap between sections, keep one side small and let the other side create the separation.
Remove Unwanted Extra Space
When you want almost no spacing, returning 0 is not always enough. In some table styles, UITableView interprets 0 as "use the default value." A common workaround is to return a tiny nonzero height:
If you also provide header or footer views, make them match:
That combination is often needed when developers ask how to remove "mysterious" section spacing.
Watch for iOS 15 Top Padding
On newer iOS versions, plain tables add top padding before section headers. If you see extra space that was not present before, this property is often the reason:
This affects the visual distance before the first section header and can make old layouts look unexpectedly loose after an OS upgrade.
Custom Views Give More Control
If the gap is part of the design, using a custom header or footer view is often better than relying only on heights. For example, you can add a spacer view with a specific background color:
This is especially useful in grouped designs where the spacing is part of the visual rhythm rather than a side effect to suppress.
Common Pitfalls
The biggest mistake is assuming there is a dedicated section-spacing property. In practice, spacing is produced indirectly by headers, footers, style defaults, and sometimes OS-level padding behavior.
Another common issue is returning 0 and expecting no space. Depending on table style, the system may substitute its default height. Use .leastNonzeroMagnitude when you need to collapse spacing reliably.
Be careful about table style. plain, grouped, and insetGrouped do not render section spacing the same way, so a fix that looks correct in one style may not match another.
Finally, on iOS 15 and later, do not ignore sectionHeaderTopPadding if the spacing suddenly changed after an upgrade.
Summary
- Section spacing in
UITableViewusually comes from header and footer heights. - Use delegate methods to add or reduce space deliberately.
- Use
.leastNonzeroMagnitudewhen returning0does not remove spacing. - Check
sectionHeaderTopPaddingon iOS 15 and later. - Use custom header or footer views when spacing is part of the design, not just a layout bug.
Related reading
- SparseArray vs HashMap
- Specifying one Dimension of Cells in UICollectionView using Auto Layout
- Spinning progress bar in every listview item
- Split a String into an array in Swift?
- Split a String into an array in Swift?
- Split an NSString to access one particular piece
- Spring Boot with Kotlin - Value annotation not working as expected
- Spring Data JPA How to use Kotlin nulls instead of Optional
.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.