On the basis of 4, override the following method control
#pragmaMark-Proxy method/** Calculate cell Row Height*/-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{/** The method of calculating the row height, how many rows are calculated when the tabular data is loaded contentsize problem: When this method executes, the cell is not instantiated! However: The row height calculation is when the cell is instantiated, by setting the Status property, the calculated = = with the status model, you can know the row height! Question: How do I get the row height before the cell is instantiated? Workaround: The row height can be calculated by status! = "Build a model that calculates the position of all controls*/Hmstatusframe*statusframe =[[Hmstatusframe alloc] init]; statusframe.status =Self.statuses[indexpath.row]; returnStatusframe. cellheight;}
HMStatusFrame.h file
#import<Foundation/Foundation.h>@classHmstatus;/** Specifically calculates all control locations*/@interfaceHmstatusframe:nsobject@property (nonatomic, assign) CGRect iconf; @property (nonatomic, assign) CGRect NAMEF; @prope Rty (nonatomic, assign) CGRect vipf; @property (nonatomic, assign) CGRect TEXTF; @property (nonatomic, assign) CGRect Pictur EF;/** Row Height*/@property (nonatomic, assign) cgfloat cellheight;/** The size of all controls can be calculated by status*/@property (nonatomic, strong) Hmstatus*status;@end
M file
#import "HMStatusFrame.h"#import "HMStatus.h"/** Name Font*/#defineKnamefont [Uifont systemfontofsize:14]/** Text Font*/#defineKtextfont [Uifont systemfontofsize:16]@implementationHmstatusframe- ( void) SetStatus: (Hmstatus * ) Status {_status=status; //0. Define SpacingCGFloat padding =Ten; //1. AvatarCGFloat IconX =padding; CGFloat Icony=padding; CGFloat iconw= -; CGFloat Iconh= -; Self.iconf=CGRectMake (IconX, Icony, Iconw, Iconh); //2. Name size is determined by the length of the text//Boundingrectwithsize calculates the area occupied by a given text string//The return value is an x, y = 0 Cgrect,w,h is the calculated width and height// //If you want to calculate the exact height of multiple rows, you need to pass in the Nsstringdrawinguseslinefragmentorigin option//Dict Dictionary that specifies the related properties of the font, the first header file in the Uikit frame//Context:nilNsdictionary *namedict =@{nsfontattributename:knamefont}; CGRect Nameframe=[Self.status.name boundingrectwithsize:cgsizemake (Maxfloat, maxfloat) options: Nsstringdrawinguseslinefragmentorigin attributes:namedict Context:nil]; Nameframe.origin.x= Cgrectgetmaxx (self.iconf) +padding; NAMEFRAME.ORIGIN.Y= padding + (self.iconf.size.height-nameframe.size.height) *0.5; SELF.NAMEF=Nameframe; //VIP iconCGFloat vipx = Cgrectgetmaxx (SELF.NAMEF) +padding; CGFloat vipy=SELF.NAMEF.ORIGIN.Y; CGFloat VIPW= -; CGFloat Viph= -; Self.vipf=CGRectMake (vipx, vipy, VIPW, VIPH); //BodyNsdictionary *textdict =@{nsfontattributename:ktextfont}; CGRect TextFrame= [Self.status.text Boundingrectwithsize:cgsizemake ( -, maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:textdict Context:nil]; Textframe.origin.x=padding; TEXTFRAME.ORIGIN.Y= Cgrectgetmaxy (self.iconf) +padding; SELF.TEXTF=TextFrame; if(Self.status.picture.length >0) { //Image MatchingCGFloat Picturex =padding; CGFloat Picturey= Cgrectgetmaxy (textFrame) +padding; CGFloat Picturew= -; CGFloat Pictureh= -; Self.picturef=CGRectMake (Picturex, Picturey, Picturew, Pictureh); Self. cellheight = Cgrectgetmaxy (self.picturef) +padding; } Else{self. Cellheight = Cgrectgetmaxy (SELF.TEXTF) +padding; }}@end
iOS eighth day (5:uitableviewcontroller Sina Weibo, calculate row height)