UITableView Adaptive Complex Cell construction ideas (this scheme is compatible with iOS8 below, if only support iOS8, there are more advanced programs, please look forward to the sequel)

Source: Internet
Author: User

For a normal cell, we can generally give a fixed height based on the type of cell, in which case the layout of our control and the amount of space needed to display it are predictable for us, so it can be implemented directly. , and for some more advanced requirements scenarios (such as the cell display of chat information), we want to show all the information in the cell, we can not return the fixed value in the proxy method of Heightforrowatindexpath.

So, our extra work steps are divided into the following stages:

Step1: Get the appropriate data content

Step2: Depending on the data content, the cell layout

Step3: Calculates the cell's final height according to the cell layout

Step4: Returns the height from the Heightforrowatindexpath.


For a picture, you can reverse the layout height of the image according to the size of the picture, for the text we need the following method (example):

cgsize constraint =cgsizemake(cell_content_width -(Cell_ Content_margin *2), 20000.0f);

cgsize size = [textsizewithfont: [uifontsystemfontofsize :font_size]constrainedtosize: Constraint linebreakmode: Uilinebreakmodewordwrap];

cgfloat height = MAX(size. Height,44.0f);


The height occupied by fixed-width text is calculated by Sizewithfont method.


A simple mathematical calculation of the height occupied by all the elements is then obtained and the cell height is returned from the delegate.


UITableView Adaptive Complex Cell construction ideas (this scheme is compatible with iOS8 below, if only support iOS8, there are more advanced programs, please look forward to the sequel)

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.