3

I have a tableView with collapsable sections. In iOS 10 or earlier, it works perfectly. In iOS 11, the tableView sections create floating section headers and duplicate section headers in the middle of other sections as seen here:

enter image description here

I've been researching this issue, and it's been reported elsewhere and discussed here

Here is my tableView code:

class CollapsibleTableViewController: UITableViewController {

    var sections = sectionsData

    override func viewDidLoad() {
        super.viewDidLoad()
        let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)


          // Auto resizing the height of the cell
        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableViewAutomaticDimension

        self.title = "Education"
                for index in 0 ..< sections.count {
                        sections[index].collapsed = true
                }
    }
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44.0
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 1.0
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension   //making this a fixed value fixes the drawing bug but doesn't let the cells autosize
}

In the other linked question I posted above, responders suggested pre-setting the estimated heights as follows:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

This fixes the duplicating sections but shrinks down my tableView cells so the label field is no longer visible and the cells don't autosize to show all of the data in them.

It's been reported that iOS 11 changes the way tableView heights work by using estimated values.

I've also tried using estimated values for all fields as follows:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.sectionHeaderHeight = UITableViewAutomaticDimension          
tableView.sectionFooterHeight = UITableViewAutomaticDimension

Nothing seems to work to both allow proper autosizing of the cells AND preventing the cells from duplicating with floating section headers.

Any thoughts on how to fix this?

  • Can you post a project with this up on GitHub so I can look at it? I have an hour to spare and would like to understand and possibly fix this. – Alex Zavatone Oct 26 '17 at 17:35
  • On iOS 10, your table would use self-sizing cells, and fixed-size headers and footers. On iOS 11, your table uses self-sizing cells, headers, and footers. Try setting `estimatedSectionHeaderHeight` and `estimatedSectionFooterHeight` to zero to go back to fixed-size headers and footers. Keep everything else the same. – Darren Oct 26 '17 at 18:06
  • 1
    @Darren - I tried that but sadly it did not help. Still get the duplicating header sections. – LearningSwift Oct 26 '17 at 20:20

1 Answers1

3

in viewWillAppear() write your tableViews initial values, than provide a UItableViewAutomaticDimension as follows:

tableView.estimatedRowHeight = 75 tableView.rowHeight = UITableViewAutomaticDimension

Rinse and repeat as necessary for header, footer, etc.

Sung
  • 434
  • 5
  • 17