Search code examples
iosswiftuitableviewfooter

How to programatically register a UITableViewHeaderFooterView in Swift 4?


I need my tableView to fire an event for:

override func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)

to do that, according to the documentation, I can't just add a view to the tableView like this:

tableView.tableFooterView = spaceView

which, whilst working, doesn't make that function get fired, nor does the table dequeue the footer despite how long the table might get.

I can't do this in the storyboard / XIB because whether a footer is added or not, is a decision only made at runtime.

So according to the documentation or at least my understanding of them, I have to make 'spaceView' a subclass of UITableViewHeaderFooterView and I have to do a formal 'register' to add it into the tableView, like this:

let spaceView = UITableViewHeaderFooterView(reuseIdentifier: "emptyFooter")

tableView.register(spaceView, forHeaderFooterViewReuseIdentifier: "emptyFooter")

But then I get a compile error:

Cannot invoke 'register' with an argument list of type '(UITableViewHeaderFooterView, forHeaderFooterViewReuseIdentifier: String)'

So, despite the docs, any ideas how I'm reading the doc's wrong?


Solution

  • Thanks for the suggestions, but none of these either addressed the problem, or led to a solution.

    Instead, I have the functionality, exactly as I wanted it, with just a small function of 6 lines of code and one private property on the view controller.

    As soon as the tableView.footerView goes 'offscreen' it's instantly set to nil, and it's only created exactly when I want it to be.