Challenge - 5 Problems
Section Header & Footer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate1:30remaining
What is the output of this code for section header title?
Given this UITableViewDataSource method, what will be shown as the header title for section 0?
iOS Swift
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == 0 ? "Fruits" : "Vegetables"
}Attempts:
2 left
💡 Hint
Check the condition for section number in the return statement.
✗ Incorrect
The method returns "Fruits" when section is 0, otherwise "Vegetables". So section 0 header is "Fruits".
❓ ui_behavior
intermediate1:30remaining
What will be the footer text for section 1 with this code?
Consider this UITableViewDelegate method. What footer text will appear for section 1?
iOS Swift
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if section == 1 {
return "End of Vegetables"
}
return nil
}Attempts:
2 left
💡 Hint
Look at the condition checking section number.
✗ Incorrect
The method returns footer text only for section 1, nil for others, so only section 1 has footer text.
❓ lifecycle
advanced1:30remaining
Which method is called to customize a section header view?
To provide a custom UIView as a section header in UITableView, which delegate method should you implement?
Attempts:
2 left
💡 Hint
Think about which method returns a UIView instead of a String.
✗ Incorrect
The method tableView(_:viewForHeaderInSection:) returns a UIView to use as the header view.
advanced
1:30remaining
What happens if you return nil in viewForFooterInSection?
If you implement tableView(_:viewForFooterInSection:) and return nil for a section, what will the table show?
Attempts:
2 left
💡 Hint
Check interaction between viewForFooterInSection and titleForFooterInSection methods.
✗ Incorrect
If viewForFooterInSection returns nil, the table uses titleForFooterInSection to show a default footer title if available.
📝 Syntax
expert2:00remaining
What error occurs with this incorrect header method signature?
Identify the error in this UITableViewDelegate method signature for section header view:
iOS Swift
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
return UIView()
}Attempts:
2 left
💡 Hint
Check the return type expected by the delegate method.
✗ Incorrect
The delegate method expects an optional UIView? return type, not a non-optional UIView.