Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

screwy tableview sections in iOS11 - UITableViewAutomaticDimension issues

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?

like image 538
LearningSwift Avatar asked Feb 12 '26 06:02

LearningSwift


1 Answers

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.

like image 134
Sung Avatar answered Feb 15 '26 01:02

Sung