Section headers and footers help organize lists by grouping items and adding titles or notes above or below each group.
Section headers and footers in iOS Swift
Introduction
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
iOS Swift
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return ["Fruits", "Vegetables"][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] } }
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.
Practice
1. What is the main purpose of section headers and footers in a UITableView?
easy
Solution
Step 1: Understand the role of section headers and footers
Section headers and footers are used to separate and label groups of rows in a table view.Step 2: Identify their effect on user experience
They improve readability by clearly marking different sections, making the list easier to navigate.Final Answer:
To organize and label groups of rows for better readability -> Option DQuick Check:
Section headers and footers = organize groups [OK]
Hint: Headers and footers label groups in lists [OK]
Common Mistakes:
- Confusing headers with animations
- Thinking headers change colors only
- Assuming headers handle input
2. Which UITableViewDataSource method is used to provide a title for a section header?
easy
Solution
Step 1: Recall UITableViewDataSource methods for headers
The method tableView(_:titleForHeaderInSection:) returns the text for the header of a section.Step 2: Differentiate from other methods
Other methods handle cells, row height, or selection, not headers.Final Answer:
tableView(_:titleForHeaderInSection:) -> Option BQuick Check:
Header title method = tableView(_:titleForHeaderInSection:) [OK]
Hint: Header titles use titleForHeaderInSection method [OK]
Common Mistakes:
- Choosing cellForRowAt for headers
- Confusing heightForRowAt with header titles
- Selecting didSelectRowAt which is for taps
3. Given this code snippet, what will be displayed as the footer text for section 1?
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if section == 0 {
return "End of first section"
} else if section == 1 {
return "End of second section"
}
return nil
}medium
Solution
Step 1: Analyze the method for section 1
The method checks if section == 1 and returns "End of second section" for that case.Step 2: Confirm no errors or nil for section 1
Since section 1 matches the else if condition, it returns the string, not nil or error.Final Answer:
"End of second section" -> Option AQuick Check:
Footer for section 1 = "End of second section" [OK]
Hint: Check section number in footer method [OK]
Common Mistakes:
- Confusing section 0 and 1
- Assuming nil means default text
- Expecting runtime error from this code
4. Identify the error in this code for adding a section header title:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "First Section"
}
}medium
Solution
Step 1: Check method return type and all paths
The method returns String? but does not return anything if section != 0, causing a missing return error.Step 2: Confirm method signature correctness
The method name and parameters are correct; the issue is missing return for other sections.Final Answer:
Missing return statement for all code paths -> Option CQuick Check:
All paths must return a value [OK]
Hint: All function paths must return a value [OK]
Common Mistakes:
- Ignoring missing return for else cases
- Thinking method name is wrong
- Confusing return type with optional
5. You want to display a custom view as a section header instead of just text. Which UITableViewDelegate method should you implement?
hard
Solution
Step 1: Identify method for custom header views
The method tableView(_:viewForHeaderInSection:) allows returning a UIView to use as a header.Step 2: Differentiate from text-only header method
tableView(_:titleForHeaderInSection:) returns only a String, not a view.Final Answer:
tableView(_:viewForHeaderInSection:) -> Option AQuick Check:
Custom header view method = viewForHeaderInSection [OK]
Hint: Use viewForHeaderInSection for custom header views [OK]
Common Mistakes:
- Using titleForHeaderInSection for views
- Confusing row height with header view
- Thinking didSelectRowAt affects headers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
