CC. CC. - 5 months ago 38
Swift Question

ios 8 swift - how to add footer for tableview using separate datasource

For 2 days I'm trying to solve this.
I just want to add a footer (custom cell) to my tableview.
I have a view with some stuff on it (labels, buttons) and I've added a tableview.
To have a clean controller, for the datasource I'm using a separate file:

class MyDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {

In my controller I have :

class MyViewController: UIViewController {

@IBOutlet weak var myButton: UIButton!
var myDatasource: MyDataSource!
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
myDatasource = MyDataSource(....)
myTableView.dataSource = myDatasource
// Do any additional setup after loading the view.

So, in
I've added:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.blackColor()
return footerView

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0

And this piece of code is not being called.
I've looked the documentation and I've found that these 2 last methods are part of
and not of part of

So, my question is, how to achieve that ?




As noted, displaying headers and footers needs to implement the relevant method of the UITableViewDelegate protocol. For the footer it is:

tableView(tableView: UITableView, viewForFooterInSection section: Int)

Make sure your table view's delegate is set correctly.

Also, note that there is another convenient table view feature: a footer that will be added below the entire table view, regardless of datasource or delegate implementation. Just assign a tableFooterView.