iOS development dynamically calculates the height of the cell

Source: Internet
Author: User

In the IOS development process , we often use UITableView, talk about UITableView of course UITableViewCell. Then sometimes we have doubts , how to make the cell height According to the size of the text , as well as the height of the photo to dynamically design it ? Let's take a look at what we can do to make the cell 's height dynamic change , so that the interface looks more visually coordinated?


Dynamically set the cell's height + (cgfloat) Heightforrowwithmodel: (Photoinfo *) photoinfo{//1. Height of picture//Let picture scale//(1) Get Picture UIImage *    Image = [UIImage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "ZZ" oftype:@ "PNG"];    CGFloat imageheight = [self heightforimage:image];    2. Height of the text cgfloat textHeight = [self heightForText:photoInfo.introduction]; 3. Return the total height of the cell return kphotocell_titlelabel_height + imageheight + textHeight + 4 * kphotocell_marginbetween;}    Calculate the height of the picture separately (CGFloat) Heightforimage: (UIImage *) image{//(2) Get the size of the picture cgsize sizes = image.size;    (3) Calculating the scaling cgfloat scale = kphotocell_width/size.width;    CGFloat imageheight = size.height * scale; return imageheight;} Calculate the height of the text individually + (cgfloat) Heightfortext: (NSString *) text{//Set the font size when calculating text, and what criteria to calculate nsdictionary *attrbute = @{nsfontattri    Butename:[uifont Systemfontofsize:kfontsize]}; return [Text Boundingrectwithsize:cgsizemake (Kphotocell_width, Options:nsstringdrawinguseslinefragmentorigin) | Nsstringdrawingusesfontleading Attributes:attrbute Context:nil].size.height;} 

In the code, K begins with the values defined by the macro.

iOS development dynamically calculates the height of the cell

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.