问题
I've added a custom header to my table view and it uses dynamic type and I want it to be self sizing.
So I added this to my viewDidLoad
:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
And this to the rest of my table view methods:
override func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 22
}
Then my header cells were way too large and not self sizing and I got this error message:
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
So I started playing around and I removed the estimatedHeightForHeaderInSection
method and implemented estimatedHeightForFooter
instead. And then my header looked perfect! But a footer started showing too, which I didn't want. So I added the heightForFooter
method and set it to 0, and now it all works like I wanted in the first place.
I'm finding this very weird, though. I'm new at all this, but I expected estimatedHeightForHeader
to work for the header and estimatedHeightForFooter
to work for the footer. Is this a bug? Am I doing something wrong? Is there a better way to accomplish what I'm trying to do?
Thanks in advance.
Daniel
Edit: I accepted Omkar's answer because it worked for me at the time, but I still didn't understand why I shouldn't use both methods. I was using a regular cell for my header, though, and now I found out that doing that causes all sorts of weird things to happen. So I just programmatically changed the UITableViewHeaderFooterView instead, and now it all works out, but I do have to implement both methods, like I first thought, otherwise the header doesn't resize properly.
回答1:
Don't use both remove this from your code
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
来源:https://stackoverflow.com/questions/33297371/estimatedheightforheaderinsection-acting-weird