IOS 8 adaptive height Cell

Source: Internet
Author: User

IOS 8 adaptive height Cell

In iOS 8, The UITableViewAutomaticDimension constant supports adaptive height cells (iOS 7 requires a lot of trouble ). But in practical application, we need to pay attention to the following issues:

1. Set the automatic layout of the template cells.
In the template cell, the automatic part of subviews must be able to fill the cell. That is to say, iOS must be able to calculate the cell height through the automatic layout constraints of the content. Take the following cell as an example:

There are two labels in the cell. The Label above has only one line of text (lines is 1), so the height will not change during running, but the Label below is multi-line text (lines is 0 ), when running, the height automatically increases according to the text content.
The automatic layout in the left figure is correct, so the cell can adapt to the height during running. This is because iOS can calculate the normal height of the cell Based on the child views in the cell contentView. The calculation method is as follows:
Cell Height = top + 1st Label heights of 1st labels + top + 2nd Label heights of 2nd labels (automatically calculated based on content) + bottom of 2nd labels

However, when we delete the top constraint (or bottom constraint) of the 2nd labels, as shown in the right figure, we have deleted the constraint at the position shown in the red box in the figure, iOS cannot calculate the height of a cell. A variable in the preceding formula is missing. In this way, when running, all cells in the table are fixed heights, and the cells overlap, and the console reports an error:

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.

2. Set tableView in viewDidLoad

Next, set tableView in viewDidLoad:
TableView. rowHeight = UITableViewAutomaticDimension
Self. tableView. estimatedRowHeight = 100;

The role of the first sentence is to enable the adaptive height feature of iOS 8 cells. The second code also provides the same function. estimatedRowHeight allows you to provide a pre-estimated cell height value. This value can be set randomly (as long as it is not 0), but if you do not write this sentence, alternatively, if you set the estimatedRowHeight attribute to 0, the automatic height of the iOS 8 cell does not take effect.

3. heightForRowAtIndexPath and estimatedHeightForRowAtIndexPath
After the automatic cell height feature of iOS 8 is enabled, these two methods are not needed, or a UITableViewAutomaticDimension constant is returned simply:
Override func tableView (tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)-> CGFloat {
Return UITableViewAutomaticDimension
}
Override func tableView (tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath)-> CGFloat {
Return UITableViewAutomaticDimension
}
Of course, completely deleting these two methods does not matter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.