Fix: Floating Section Headers in UITableView Plain Style on iOS

Written by

in

Quick answer: To disable floating section headers in a plain UITableView, either add a dummy tableHeaderView with a negative contentInset equal to its height, or replace section headers with custom cells in a single section. Both approaches make headers scroll normally like regular cells.

Why this happens

In UITableViewStylePlain, section headers float (stick to the top) while their section content scrolls underneath. This is the default behavior Apple designed for plain-style tables — think Contacts or Settings — where the header acts as a persistent label for the current section. There is no built-in property to turn this off; the style itself enforces it.

Grouped style (UITableViewStyleGrouped) keeps headers static, but it also adds rounded corners, margins, and a background appearance that many layouts don’t want. Developers often need full-width, edge-to-edge cells without the grouped styling, yet still want headers to scroll away like normal rows.

How to fix it

1. Dummy tableHeaderView with negative contentInset (quickest)

This trick pushes the first section header up behind a zero-height (or fixed-height) table header view, then uses a negative top inset to pull content up so the first header starts at the top. Once scrolling begins, all section headers behave like regular rows.

Swift

let dummyHeight: CGFloat = 40 // match your header height
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: dummyHeight))
tableView.contentInset = UIEdgeInsets(top: -dummyHeight, left: 0, bottom: 0, right: 0)

Objective-C

CGFloat dummyHeight = 40;
UIView *dummy = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyHeight)];
self.tableView.tableHeaderView = dummy;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyHeight, 0, 0, 0);

Notes
– Set dummyHeight to the exact height your tableView(_:heightForHeaderInSection:) returns.
– If header heights vary, use the tallest one or compute dynamically.
– Works on iOS 7+; no delegate changes required.

2. Replace section headers with custom cells (most control)

Move header content into actual cells. Use a single section and return a distinct cell type for header rows. This gives you full layout freedom — animations, selection, editing — because they’re just cells.

Swift

enum RowType {
    case header(String)
    case item(ItemModel)
}

var rows: [RowType] = [] // build by interleaving headers and items

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    rows.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch rows[indexPath.row] {
    case .header(let title):
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
        cell.textLabel?.text = title
        cell.backgroundColor = .systemBackground // or whatever you need
        return cell
    case .item(let model):
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
        // configure item cell
        return cell
    }
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    0 // or .leastNonzeroMagnitude to hide completely
}

When to choose this
– You need headers to participate in swipe actions, editing, or animations.
– Header heights vary per section.
– You want to avoid the negative-inset layout quirk (e.g., scroll indicator offset).

How to prevent it

  • Decide on header behavior before choosing table style. If headers must scroll, default to the single-section + custom-cell pattern.
  • If you inherit a plain-style table that later needs non-floating headers, apply the dummy-header fix as a localized patch rather than rewriting the data source.
  • Test on both iPhone and iPad; split-view controllers can reveal inset issues on iPad.

FAQ

Does the dummy-header approach affect scroll indicators?

Yes. The vertical scroll indicator will start dummyHeight points lower. If this is visible, set tableView.scrollIndicatorInsets = tableView.contentInset to match.

Can I use UITableViewDiffableDataSource with the custom-cell approach?

Absolutely. Define a SectionIdentifier with a single section and ItemIdentifier as an enum (header vs. item). The snapshot builds the same interleaved array.

What about sectionIndexTitles?

They still work with the dummy-header fix because sections exist. With the single-section custom-cell approach, you lose the index bar — plan accordingly if you need A–Z scrolling.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.