There are many third-class libraries in GitHub that are well-packaged, with a class library of adaptive Cell Heights
: Https://github.com/gsdios/SDAutoLayout
Model class
Commentsmodel
#import " JSONModel.h " #import " getCommentData.h " @interface Commentsmodel:jsonmodel@property (nonatomic,copy) nsarray<getCommentData> *commentlist; @end
#import " commentsModel.h " @implementation Commentsmodel @end
Getcommentdata
#import " @protocol Getcommentdata @end @interface Getcommentdata:jsonmodel@property (nonatomic,copy) nsstring *message; @property (nonatomic,copy) nsstring *nickname; @property (Nonatomic, copy) nsstring *CREATETIMESTR; @end
#import " getCommentData.h " @implementation Getcommentdata @end
Controller
#import "CommentsTableViewController.h"#import "commentsModel.h"#import "commentCell.h"@interfaceCommentstableviewcontroller () @property (nonatomic,strong) Nsarray*Commentsarray;@end@implementationCommentstableviewcontroller-(Nsarray *) commentsarray{if(_commentsarray==Nil) {NSData*data=[nsdata Datawithcontentsoffile:[[nsbundle Mainbundle] Pathforresource:@"Comment_list.json"Oftype:nil]]; Commentsmodel*commensm=[[Commentsmodel alloc]initwithdata:data Error:nil]; _commentsarray=commensm.commentlist; } return_commentsarray;}- (void) viewdidload {[Super viewdidload]; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark-table View Data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return 1;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {returnSelf.commentsArray.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *id=@"Comment"; Commentcell*cell=[TableView Dequeuereusablecellwithidentifier:id]; if(cell==Nil) {Cell=[[Commentcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:id]; } cell.commentdata=Self.commentsarray[indexpath.row]; returncell;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return[self Cellheightforindexpath:indexpath cellcontentviewwidth:[self cellcontentviewwith];}-(cgfloat) cellcontentviewwith{cgfloat width=[UIScreen mainscreen].bounds.size.width; if([uiapplication sharedapplication].statusbarorientation! = uiinterfaceorientationportrait && [[UIDevice Currentdevice].systemversion Floatvalue] <8) {width=[UIScreen mainscreen].bounds.size.height; } returnwidth;}@end
Code for the specific custom cell
#import <UIKit/UIKit.h>@class getcommentdata; @interface * * * * *Datelabel; @end
#import "commentCell.h"#import "commentsModel.h"@implementationCommentcell-(void) Setcommentdata: (Getcommentdata *) commentdata{_commentdata=Commentdata; _titlelabel.text=Commentdata.message; _datelabel.text=Commentdata.createtimestr; _namelabel.text=Commentdata.nickname;}-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ Self=[Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if(self) {[self setup]; } returnSelf ;}-(void) setup{_namelabel=[uilabelNew]; [Self.contentview Addsubview:_namelabel]; _namelabel.textcolor=[uicolor colorwithred:0.891Green0.549Blue0.073Alpha1.000]; _namelabel.font=[uifont systemfontofsize: the]; _namelabel.numberoflines=1; _titlelabel=[uilabelNew]; [Self.contentview Addsubview:_titlelabel]; _titlelabel.textcolor=[Uicolor Darkgraycolor]; _titlelabel.font=[uifont systemfontofsize: the]; _titlelabel.numberoflines=0; _datelabel=[uilabelNew]; [Self.contentview Addsubview:_datelabel]; _datelabel.textcolor=[uicolor colorwithred:0.679Green0.166Blue0.828Alpha1.000]; _datelabel.font=[uifont systemfontofsize: the]; _datelabel.numberoflines=1; CGFloat margin=Ten; UIView*contentview=Self.contentview; _namelabel.sd_layout. Leftspacetoview (Contentview,margin). Topspacetoview (Contentview,margin). RightSpaceToView (c Ontentview,margin). Heightis ( -); _titlelabel.sd_layout. Leftspacetoview (Contentview,margin). Topspacetoview (_namelabel,2). Rightspacetoview (Contentview,margin). Autoheightratio (0); _datelabel.sd_layout. Leftspacetoview (Contentview,margin). Topspacetoview (_titlelabel,5). Heightis ( -). Widthis ( Max); [Self Setupautoheightwithbottomviewsarray:@[_titlelabel,_datelabel,_namelabel] bottommargin:margin];}- (void) awakefromnib {}- (void) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated];}@end
IOS Tableviewcell Adaptive Height Third class library