0
0
iOS Swiftmobile~5 mins

Section headers and footers in iOS Swift

Choose your learning style9 modes available
Introduction

Section headers and footers help organize lists by grouping items and adding titles or notes above or below each group.

You want to separate a list of contacts by their first letter.
You need to add a title above each group of settings in a settings screen.
You want to show extra information or summary below a group of items.
You want to make a long list easier to scan by grouping related items visually.
Syntax
iOS Swift
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Header Title"
}

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return "Footer Title"
}
These methods belong to UITableViewDataSource protocol.
Return nil if you don't want a header or footer for that section.
Examples
Returns different header titles for two sections: "Fruits" and "Vegetables".
iOS Swift
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return ["Fruits", "Vegetables"][section]
}
Shows a footer with the section number below each section.
iOS Swift
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return "End of section \(section + 1)"
}
Sample App

This example shows a table with two sections: "Fruits" and "Vegetables". Each section has a header title and a footer note. The table displays the items in each group.

iOS Swift
import UIKit

class ViewController: UIViewController, UITableViewDataSource {
    let tableView = UITableView()
    let data = [["Apple", "Banana"], ["Carrot", "Lettuce"]]
    let headers = ["Fruits", "Vegetables"]
    let footers = ["End of fruits", "End of vegetables"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.frame = view.bounds
        tableView.dataSource = self
        view.addSubview(tableView)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = data[indexPath.section][indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return headers[section]
    }

    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return footers[section]
    }
}
OutputSuccess
Important Notes

Headers and footers are optional; return nil to omit them.

For more custom headers or footers, use UITableViewDelegate methods to provide views instead of just text.

Headers appear above the section rows; footers appear below.

Summary

Section headers and footers help organize and label groups in a list.

Use UITableViewDataSource methods to add simple text headers and footers.

They improve readability and user experience in long or grouped lists.