iOS 8 supports adaptive height of cells with Uitableviewautomaticdimension constants (iOS 7 is much more troublesome). But in practical applications, we need to pay attention to the following issues:
1. Set up automatic layout of template cells
In the template cell, the Subviews auto-local must be able to fill the cell. In other words, IOS must be able to calculate the cell's height through the automatic layout constraints of the content. Take the following cell as an example:
There are up to two labels in the cell, the label above has only one line of text (lines is 1), so the height does not change at run time, but the following label is a multiple-line text (lines is 0), and its height will automatically grow based on the content of the texts.
The automatic layout in the left image is correct, so the run-time cells can be self-adapting to heights. This is because iOS calculates the normal height of the cell based on the individual sub-view in the cell's Contentview, calculated as:
Cell height = 1th label top+ 1th label height + 2nd label top+ 2nd label height (automatically calculated based on content) + 2nd label Bottom
But when we delete the top constraint (or bottom constraint) of the 2nd label, as shown in the image on the right, we have removed the constraint of the position shown in the red box, and iOS cannot calculate the height of the cell. Because one of the variables in the above formula is missing. This allows all cells in the run-time table to be fixed heights, overlapping between cells, and the console will error:
Warning once only:detected a case where constraints ambiguously suggest a height of zero for a tableview cell ' s content V Iew. We ' re considering the collapse unintentional and using standard height instead.
2. Set the TableView in Viewdidload
Next, set the TableView in Viewdidload:
Tableview.rowheight = Uitableviewautomaticdimension
Self.tableView.estimatedRowHeight = 100;
The function of the first sentence, as previously described, is the adaptive height feature of the cell that opens iOS 8. The second code is the same function, estimatedrowheight let you provide a pre-estimate cell height value, this value can be randomly set (as long as not 0), but if you do not write this sentence, or set the Estimatedrowheight property to 0, the iOS The cell auto Height feature of 8 does not take effect.
3. Heightforrowatindexpath method and Estimatedheightforrowatindexpath method
When you turn on the Automatic cell height feature for iOS 8, these two methods are not required, or simply return a Uitableviewautomaticdimension constant:
Override Func TableView (Tableview:uitableview, Heightforrowatindexpath indexpath:nsindexpath), CGFloat {
Return uitableviewautomaticdimension
}
Override Func TableView (Tableview:uitableview, Estimatedheightforrowatindexpath indexpath:nsindexpath), cgfloat {
Return uitableviewautomaticdimension
}
Of course, it doesn't matter if the two methods are completely removed.
IOS 8 Adaptive Height cell problem