0
0
iOS Swiftmobile~20 mins

Section headers and footers in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Section Header & Footer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1: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"
}
A"Fruits" for section 0, "Vegetables" for section 1
B"Vegetables" for section 0, "Fruits" for section 1
CNo header titles shown
DRuntime error due to missing return
Attempts:
2 left
💡 Hint
Check the condition for section number in the return statement.
ui_behavior
intermediate
1: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
}
A"End of Vegetables" for all sections
B"End of Vegetables" for section 1 footer
CNo footer text for any section
DApp crashes due to nil return
Attempts:
2 left
💡 Hint
Look at the condition checking section number.
lifecycle
advanced
1: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?
AtableView(_:titleForHeaderInSection:)
BtableView(_:heightForHeaderInSection:)
CtableView(_:viewForHeaderInSection:)
DtableView(_:willDisplayHeaderView:forSection:)
Attempts:
2 left
💡 Hint
Think about which method returns a UIView instead of a String.
navigation
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?
ADefault footer title is shown if titleForFooterInSection is implemented
BNo footer view is shown for that section
CApp crashes with nil view error
DFooter view from previous section is reused
Attempts:
2 left
💡 Hint
Check interaction between viewForFooterInSection and titleForFooterInSection methods.
📝 Syntax
expert
2: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()
}
ANo error, code compiles and runs
BMethod name is incorrect
CReturn type UIView is invalid, should be String
DMissing optional return type causes compile error
Attempts:
2 left
💡 Hint
Check the return type expected by the delegate method.