UITableView with fixed section headers
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A UITableView can keep section headers visible while scrolling, but the behavior depends on table style and on what kind of header you provide. In a plain-style table view, section headers naturally pin to the top until the next section pushes them away. Most confusion comes from using the wrong table style or customizing the header view in a way that fights the built-in behavior.
Use .plain Table Style for Pinned Headers
If you want the normal iOS sticky section-header behavior, create the table view with .plain.
With .plain, the current section header sticks at the top automatically. You do not need to implement scroll-view hacks for the default behavior.
Custom Header Views Still Pin
If you need a custom appearance, implement viewForHeaderInSection instead of relying on the default title label.
The header view will still pin in a plain table view. The important part is that you are customizing the section header, not adding a separate overlay view that only looks like one.
Know the Difference Between Section Headers and Table Header View
tableHeaderView is not the same thing as a section header. It sits above the entire table and scrolls with the content. If you want one header for each section that pins while that section is active, use section-header delegate methods.
That distinction explains a lot of bugs. Developers often add a tableHeaderView, expect sticky section behavior, and then spend time debugging behavior that is actually working exactly as designed.
Grouped Style Behaves Differently
A grouped or inset-grouped table view does not behave the same way visually as a plain table view. Depending on the iOS version and layout, grouped headers may not give the same pinned presentation you expect from plain tables.
If the product requirement is specifically "sticky section headers like Contacts", start with .plain and only move away from it if the visual design truly requires a grouped appearance.
Auto Layout and Sizing Still Matter
If a custom header view has ambiguous layout or an incorrect height, the table view can display awkward spacing or clipped content. Keep header views simple, set an explicit height when needed, and avoid heavy subview hierarchies unless the design genuinely needs them.
Pinned headers are a built-in behavior. Most rendering problems around them come from custom layout decisions rather than from the table view itself.
Common Pitfalls
- Using
tableHeaderViewwhen the requirement is really a per-section sticky header. - Expecting grouped style to behave like plain style for pinned headers.
- Adding scroll hacks for behavior that
.plainalready provides. - Returning a custom header view without matching height information.
- Overcomplicating header layout and then blaming
UITableViewfor clipping or spacing issues.
Summary
- Use
.plainstyle when you want the standard pinned section-header behavior. - Implement
titleForHeaderInSectionorviewForHeaderInSectionfor section headers. - '
tableHeaderViewis a different feature and does not replace section headers.' - Keep custom header sizing and layout explicit.
- Start with built-in table behavior before adding custom scroll logic.
Related reading
- UITableview with more than One Custom Cells with Swift
- UITableViewCell Separator disappearing in iOS7
- UITableViewCell, show delete button on swipe
- UITableViewCell show white background and cannot be modified on iOS7
- UITableViewCell subview disappears when cell is selected
- UITableViewCell with UITextView height in iOS 7?
- UITableViewHeaderFooterView Unable to change background color
- UITapGestureRecognizer - make it work on touch down, not touch up?
.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.