Approach 1: Creating a footer view with UIView
in code
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerRect = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 40.0)
let footerView = UIView(frame: footerRect)
footerView.backgroundColor = UIColor.green
let label = UILabel(frame: CGRect(x: 0.0, y: 4.0, width: 200.0, height: 20.0))
label.text = "Hello"
footerView.addSubview(label)
return footerView
}
Approach 2: Creating a footer view with an IBOutlet-connected UIView
(.xib) object
a) Create a View file by choosing File > New > File. Select View under User Interface. Name a file. For example, name FooterView.xib.
b) Create a UIView
subclass file by choosing File > New > File. Select Cocoa Touch Class under Source. Name the file after selecting the UIView subclass. For example, name FooterView.swift.
c) Select the View
file. And select File's Owner
in the middle pane. Then set the UIView
subclass name (FooterView) as the class. Open both the View
file and the UIView subclass
file. Make an IBOutlet connection of the latter to the Content View
of the former.
import UIKit
class FooterView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var myLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("FooterView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
}
override func awakeFromNib() {
super.awakeFromNib()
myLabel.text = "My footer"
}
}
d) Add a UIView
object to the view controller. (See the picture below.) Set the class name (FooterView).

e) Wire the footer view object to the view controller.
f) Prepare for table view's viewForFooterInSection
delegate method.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// MARK: - Variables
let list = ["George", "Nancy", "Jim"]
// MARK: - IBOutlet
@IBOutlet var footerView: FooterView!
@IBOutlet weak var tableView: UITableView!
// MARK: - IBAction
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerRect = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 40.0)
footerView.frame = footerRect
return footerView
}
}
