There are times when the height of the UITableViewCell per row is not fixed and needs to be set dynamically.
UITableView has an agent method,
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{ return (( czweiboframe*) [Self.weiboframes ObjectAtIndex:indexPath.row]). rowHeight; }
Some would like to get the cell here first, and then return to the height of the cell, but this is not feasible, because the above method will be
Executed before the following method.
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ Czweiboframe *model = Self.weiboframes[indexpath.row]; static NSString *identifer = @ "Weibo"; Czweibocell *cell = [TableView dequeuereusablecellwithidentifier:identifer]; if (cell = = nil) { cell = [[Czweibocell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Identifer]; } Cell.weiboframe = model; return cell;}
This means that you must set the cell's height before you get the cell.
So what to do, then, when you set the data to the cell, the height is now calculated in the data model.
Here is the structure of the Czweiboviewcell model in the code above
@interface czweiboframe:nsobject@property (nonatomic,strong) Czweibo *weibo; @property (Nonatomic,assign, ReadOnly) CGRect Iconframe, @property (nonatomic,assign,readonly) cgrect textFrame; @property (Nonatomic, assign,readonly) CGRect nameframe, @property (nonatomic,assign,readonly) cgrect pictureframe; @property ( nonatomic,assign,readonly) CGRect vipframe; @property (nonatomic,assign,readonly) cgfloat rowHeight; @end
Where Weibo is the data to be displayed for each row, row height is line height, the rest is the frame of each control in the cell, and the frame is computed in the Weibo set method
-(void) Setweibo: (Czweibo *) weibo{_weibo = Weibo; CGFloat margin = 10; CGFloat iconw = 35; CGFloat Iconh = 35; CGFloat IconX = margin; CGFloat icony = margin; _iconframe = CGRectMake (IconX, Icony, Iconw, Iconh); CGFloat NameX = Cgrectgetmaxx (_iconframe) +margin; Dynamically calculates the size//header file nsattributstring nsdictionary *attr = @{nsfontattributename:namefont} based on the contents of the text in Labele; Cgsize namesize = [self sizewithtext:_weibo.name size:cgsizemake (maxfloat, maxfloat) Font:namefont]; CGFloat Namew = namesize.width; CGFloat Nameh = namesize.height; CGFloat Namey = Icony + (Iconh-nameh)/2; _nameframe = CGRectMake (NameX, Namey, Namew, Nameh); CGFloat VIPW = 10; CGFloat viph = 10; CGFloat vipx = Cgrectgetmaxx (_nameframe) +margin; CGFloat vipy = Icony + (ICONH-VIPH)/2; _vipframe = CGRectMake (vipx, vipy, VIPW, VIPH); CGFloat textx = IconX; CGFloat texty = Cgrectgetmaxy (_iconframe) +margin; Cgsize s = Cgsizemake ([[UIScreen MainScreen] bounds].size.width-margin*2, maxfloat); Cgsize textSize = [self sizeWithText:weibo.text size:s font:textfont]; _textframe = CGRectMake (Textx, texty, Textsize.width, textsize.height); CGFloat PICW = 200; CGFloat PicH = 200; CGFloat picx = IconX; CGFloat picy = Cgrectgetmaxy (_textframe) +margin; _pictureframe = CGRectMake (Picx, Picy, PICW, PicH); _rowheight = 0; if (self.weibo.picture) {_rowheight = Cgrectgetmaxy (_pictureframe) +margin; }else{_rowheight = Cgrectgetmaxy (_textframe) +margin; }}
Gets the string size-(cgsize) Sizewithtext: (NSString *) Text size: (cgsize) Size font: (Uifont *) font{ nsdictionary *dict = @ {Nsfontattributename:font}; Cgsize sz = [text boundingrectwithsize:size options:nsstringdrawinguseslinefragmentorigin attributes:dict context:nil ].size; return sz;}
And then we'll be able to set the height for each line.
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{ return (( czweiboframe*) [Self.weiboframes ObjectAtIndex:indexPath.row]). Rowheight;<span style= "Font-family:arial, Helvetica, Sans-serif; " > </span>
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Setting the UITableViewCell height problem