Bird
Raised Fist0
iOS Swiftmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of section headers and footers in a UITableView?
easy
A. To change the background color of the table view
B. To add animations between table cells
C. To handle user input events
D. To organize and label groups of rows for better readability

Solution

  1. 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.
  2. Step 2: Identify their effect on user experience

    They improve readability by clearly marking different sections, making the list easier to navigate.
  3. Final Answer:

    To organize and label groups of rows for better readability -> Option D
  4. Quick 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
A. tableView(_:cellForRowAt:)
B. tableView(_:titleForHeaderInSection:)
C. tableView(_:heightForRowAt:)
D. tableView(_:didSelectRowAt:)

Solution

  1. Step 1: Recall UITableViewDataSource methods for headers

    The method tableView(_:titleForHeaderInSection:) returns the text for the header of a section.
  2. Step 2: Differentiate from other methods

    Other methods handle cells, row height, or selection, not headers.
  3. Final Answer:

    tableView(_:titleForHeaderInSection:) -> Option B
  4. Quick 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
A. "End of second section"
B. "End of first section"
C. nil (no footer text)
D. Runtime error

Solution

  1. Step 1: Analyze the method for section 1

    The method checks if section == 1 and returns "End of second section" for that case.
  2. 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.
  3. Final Answer:

    "End of second section" -> Option A
  4. Quick 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
A. Using String instead of String?
B. Wrong method name for header title
C. Missing return statement for all code paths
D. Incorrect parameter types

Solution

  1. 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.
  2. Step 2: Confirm method signature correctness

    The method name and parameters are correct; the issue is missing return for other sections.
  3. Final Answer:

    Missing return statement for all code paths -> Option C
  4. Quick 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
A. tableView(_:viewForHeaderInSection:)
B. tableView(_:titleForHeaderInSection:)
C. tableView(_:heightForRowAt:)
D. tableView(_:didSelectRowAt:)

Solution

  1. Step 1: Identify method for custom header views

    The method tableView(_:viewForHeaderInSection:) allows returning a UIView to use as a header.
  2. Step 2: Differentiate from text-only header method

    tableView(_:titleForHeaderInSection:) returns only a String, not a view.
  3. Final Answer:

    tableView(_:viewForHeaderInSection:) -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard