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