Keep each section's title and rows together in one model, return sections.count from numberOfSections(in:), and build custom headers from a registered UITableViewHeaderFooterView in tableView(_:viewForHeaderInSection:). Don't reuse a table cell as a header. For plain text, tableView(_:titleForHeaderInSection:) is enough. Headers only appear if your class is the table's delegate, which a UITableViewController already is.
SWIFT TIPS · UIKIT
First posted on June 26, 2018 for Swift 4, as "Use Header In Section [TableViewCell] Swift 4". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target, but not run on a device. The data check was run on a Mac with Swift 6.2.3.
In 2018 I wanted a table of Apple products grouped by device, with a header above each group. The post quoted a Stack Overflow explanation of why viewForHeaderInSection wasn't being called, then showed a UITableViewController with section names in one array, rows in another, and a header made by dequeuing a table cell from a XIB.
The headers worked, but the setup had two traps, and one of them means the code no longer compiles. Here is the version I'd write now.
Sections with headers
import UIKit
struct ProductSection {
let title: String
let items: [String]
}
final class ProductsViewController: UITableViewController {
private let sections = [
ProductSection(title: "iPhone", items: ["iPhone 5", "iPhone 5s", "iPhone 6"]),
ProductSection(title: "iPad", items: ["iPad mini", "iPad Air 2", "iPad Pro"]),
ProductSection(title: "Apple Watch", items: ["Apple Watch", "Apple Watch Series 2"]),
]
private let headerID = "header"
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Apple Products"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: headerID)
}
override func numberOfSections(in tableView: UITableView) -> Int { sections.count }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
sections[section].items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = sections[indexPath.section].items[indexPath.row]
cell.contentConfiguration = content
return cell
}
// A reusable header view, not a cell
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID)
var content = UIListContentConfiguration.prominentInsetGroupedHeader()
content.text = sections[section].title
content.secondaryText = "\(sections[section].items.count) models"
header?.contentConfiguration = content
return header
}
}
One model per section
The 2018 code kept names and rows in parallel arrays: four section names and three groups of rows. I ran that data through a loop over the names, the way a header method would read it:
names: 4 item groups: 3
section 0: iPhone has 6 rows
section 1: iPad has 4 rows
section 2: Apple Watch has 3 rows
Fatal error: Index out of range
The fourth name has no rows to go with it, and reading the fourth group stops the program. The 2018 table happened to avoid the crash because numberOfSections returned the number of row groups, three, so the extra "Apple Watch" name was simply never shown. Change that line to count the names and the screen crashes. A ProductSection struct that holds the title and its rows together makes that mismatch impossible.
Use a header view, not a cell
Dequeuing a UITableViewCell and returning it as a header is a well-worn trick, and it usually looks fine. UIKit has a class made for the job, though: UITableViewHeaderFooterView, which Apple describes as a reusable view for the top or bottom of a section. Register it once with register(_:forHeaderFooterViewReuseIdentifier:), dequeue it in viewForHeaderInSection, and set its content.
The code uses a content configuration, UIListContentConfiguration.prominentInsetGroupedHeader(), which gives the header the system's text style and margins, plus a secondary line with the number of models. Because the font comes from the system, it follows the user's text size setting. For more control, subclass UITableViewHeaderFooterView and add your own views to its contentView.
If all you need is a line of text, skip the custom view and return the title from tableView(_:titleForHeaderInSection:).
Why the header method wasn't called
The Stack Overflow answer the 2018 post quoted made a real point: viewForHeaderInSection belongs to UITableViewDelegate, while the row methods belong to UITableViewDataSource. If a plain view controller sets only tableView.dataSource = self, rows appear but the header method never runs. Set tableView.delegate = self as well, or connect the delegate outlet in the storyboard. A UITableViewController is both by default, which is why the code above needs neither line.
With a header view built this way you usually don't need heightForHeaderInSection either, because the table can size the header from its content. That matters when the user picks a large text size. The 2018 version fixed the height at 34 points, which can clip larger text.
What the 2018 version said
Type-checked as posted, the code fails in both Swift 5 and Swift 6 mode. Part of that is expected, because ApiTableViewCell came from the XIB in my project. The other errors are in the code itself: sectionItems is an array of string arrays, but the row methods read sectionItems[section].name, which gives "value of type '[String]' has no member 'name'". The model struct above removes the need for that.
Keep reading on JR Devhub
Comments