1

I have created UIViewController and added UITableView on it (pinned to all four edges with autolayout).

Then I set estimatedRowHeight (44) and rowHeight (UITableViewAutomaticDimension) and returned 5 custom cells. And it worked.

Now, I want to add custom UITableViewHeaderFooterView that would have dynamic height.

I'm doing next:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 88.0
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return R.nib.orderStatusHeaderView.firstView(owner: self)!
}

My OrderStatusHeaderView is a xib view that has UITableView on it pinned to all 4 edges with autolayout.

OrderStatusHeaderView:

final class OrderStatusHeaderView: UITableViewHeaderFooterView {
    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.dataSource = self
            tableView.delegate = self
            tableView.rowHeight = 44.0
        }
    }
}
extension OrderStatusHeaderView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "\(indexPath.row)")
        cell.textLabel?.text = "\(indexPath.row)"
        cell.backgroundColor = .red
        return cell
    }
}

This displays like:

enter image description here

And when I tap or scroll, all red cells disappears. What could it be? And how to make UITableView dynamically load content and UITableViewHeaderFooterView will size itself so it fit UITableView.contentSize. Is it possible?

Olexiy Pyvovarov
  • 870
  • 2
  • 17
  • 32

2 Answers2

2

Check out:

tableView(_:estimatedHeightForFooterInSection:)

and

tableView(_:heightForFooterInSection:)

You also have the equilavant for headerInSection.

  • Yes, it seems great. I've made it to display. But the problem is that: In `tableView(_:heightForFooterInSection:)` I return `header.tableView.contentSize.height`. But the content is not calculated yet, it uses `header.tableView` estimated height for row and calculates the overall `contentSize.height` as `numberOfRows * estimatedRowHeight`. But the actual size of row is `UITableViewAutomaticDimensions`. – Olexiy Pyvovarov Feb 03 '17 at 11:30
  • 1
    @OlexiyPyvovarov The point of this method is to know what to set as "contentSize" height (cell height), so asking it to set the contentSize height as the height of course returns 0. If you want to "autosize" you need to either go with UITableViewAutomaticDimensions or calculate the height manually in a method and return it there. –  Feb 03 '17 at 11:48
  • the size is not calculated because the rows haven't been displayed yet, I suppose. But I manually created a method that calculates the whole content size with dynamic rows height and it worked Thanks! – Olexiy Pyvovarov Feb 03 '17 at 14:09
  • 1
    @OlexiyPyvovarov Great! I recommend you keep it that, in my opinion the automaticdimension method is not that very precise or effective, maybe works for some stuff tho, dunno. –  Feb 03 '17 at 14:29
0

Since you are asking for a table's header and footer view, you can skip the delegate methods you describe. Those (as the name implies) are for SECTION headers and footers.

When you set a view that is using AutoLayout as the table's header or footer, the its frame still has a zero height (That's why buttons in such view for example won't work as they are not receiving the touches).

To correctly size a table's header or footer views using AutoLayout you have to apply a trick to actually calculate the height yourself, and set the headerView again. It is described in detail in many posts like these:

https://stackoverflow.com/a/28102157/756039 https://gist.github.com/marcoarment/1105553afba6b4900c10 http://collindonnell.com/2015/09/29/dynamically-sized-table-view-header-or-footer-using-auto-layout/ http://roadfiresoftware.com/2015/05/how-to-size-a-table-header-view-using-auto-layout-in-interface-builder/

Hope this helps.

Community
  • 1
  • 1
Thomas Krajacic
  • 2,488
  • 1
  • 21
  • 25