All uitableviewcells become one row in height on iOS 9

I have an application with multiple UITableView controllers. By launching iOS 8.x, the height of all cells in each table will be resized to fit the contents of the cell (all of them contain only UILabel with plain text). Now, working on iOS 9, each cell on each table has only one row. This is with both dynamic and static tables. I looked at the UIKit diff document and did an extensive search, but I can't find the right combination of things to get everything except the height of one row in all cells in all tables.

+4
source share
2 answers

I came across a similar case. It seems like the trick is to implement dynamic measurement using UITableViewdelegate methods explicitly. Even if the settings automaticin the storyboards should work, this is not so. The solution is to explicitly provide UITableViewAutomaticDimensionthrough the delegation method, and then provide the estimated cell sizes, as usual:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    /* Return an estimated height or calculate 
     * estimated height dynamically on information 
     * that makes sense in your case.
     */
    return 200.0f;
}

If someone knows exactly why , this is necessary in iOS 9 and how it differs from iOS 8, I would like to hear it.

+11
source

We can use the "UITableViewAutomaticDimension" in iOS 8 to explicitly use dynamic size using the following two properties: -

tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension

Then add the following code to the cellForRowAtIndexPath method in front of the return cell.

cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()

iOS 9 tableView: -

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    }
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
0

Source: https://habr.com/ru/post/1606996/


All Articles