UITableView
iOS Development
Swift Programming
UI Design
Mobile App Development

Space Between Sections in UITableView

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

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, or insetGrouped tables

You can control those values through delegate methods:

swift
1class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
2    @IBOutlet weak var tableView: UITableView!
3
4    func numberOfSections(in tableView: UITableView) -> Int {
5        return 3
6    }
7
8    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
9        return 2
10    }
11
12    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
13        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
14        cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"
15        return cell
16    }
17
18    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
19        return 8
20    }
21
22    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
23        return 8
24    }
25}

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:

swift
1func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
2    return .leastNonzeroMagnitude
3}
4
5func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
6    return .leastNonzeroMagnitude
7}

If you also provide header or footer views, make them match:

swift
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return UIView(frame: .zero)
}

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:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3
4    if #available(iOS 15.0, *) {
5        tableView.sectionHeaderTopPadding = 0
6    }
7}

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:

swift
1func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
2    let spacer = UIView()
3    spacer.backgroundColor = .clear
4    return spacer
5}
6
7func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
8    return 16
9}

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 UITableView usually comes from header and footer heights.
  • Use delegate methods to add or reduce space deliberately.
  • Use .leastNonzeroMagnitude when returning 0 does not remove spacing.
  • Check sectionHeaderTopPadding on iOS 15 and later.
  • Use custom header or footer views when spacing is part of the design, not just a layout bug.

Course illustration
Course illustration

All Rights Reserved.