Complete the code to set a section header title in a UITableView.
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return [1]
}The method titleForHeaderInSection returns a string to display as the section header title. Here, we return a fixed string.
Complete the code to provide a custom UIView as a section footer in UITableView.
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
footerView.backgroundColor = [1]
return footerView
}The viewForFooterInSection method returns a UIView to display as the footer. Setting the background color to UIColor.red makes it visible.
Fix the error in this code to set the height for a section header.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return [1]
}The method expects a CGFloat value, so returning the number 50 without quotes is correct. Returning a string causes an error.
Fill both blanks to create a UILabel for a section header view with text and color.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = [1]
label.textColor = [2]
return label
}The label's text should be a string like "Header Title" and the text color a UIColor like UIColor.blue.
Fill all three blanks to create a footer view with a UILabel showing section number and custom color.
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
let label = UILabel()
label.text = [1]
label.textColor = [2]
footer.addSubview(label)
footer.backgroundColor = [3]
return footer
}The label text uses string interpolation to show the section number. The label text color is UIColor.darkGray and the footer background color is UIColor.lightGray.