UITableview with dynamic height cells & filling footer view

Viewed 2073

I am trying to make a UITableview with a footer view that fills the remaining empty space of the UITableview. However, the catch is that the cells have varying heights.

I am aware that, if the cells were all a static height, I could simply count how many cells there are and multiply that by the static height. Unfortunately, that won't work in this case.

Current solution:

Works for static height cells.

Does not work for dynamic height cells.

    private func adjustFooterHeight() {
        if data.count == 0 {
            footerView.frame.size.height = tableView.bounds.height
        }
        else {
            //I use data.count instead of (data.count - 1) as I have the extra hidden cell, as pictured in image 2.
            let bottomFrame = tableView.rectForRow(at: IndexPath(row: data.count, section: 0))
            let height = tableView.bounds.height - (bottomFrame.origin.y + bottomFrame.height - rowHeight)
            footerView.frame.size.height = height < 0 ? 0 : height
            tableView.reloadData()
        }
    }

Diagram of Intentions:

enter image description here

1 Answers
Related