Detailed IOS Tableviewcell Adaptive high Third class library _ios

Source: Internet
Author: User
Tags uikit

In GitHub, there are many third-class libraries packaged by Daniel, which have a class library with adaptive cell heights.

Download Address: 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 " JSONModel.h "
@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

Controller

#import "CommentsTableViewController.h" #import "commentsModel.h" #import "CommentCell.h" @interface
Commentstableviewcontroller () @property (Nonatomic,strong) Nsarray *commentsarray; @end @implementation Commentstableviewcontroller-(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];//DISPO
SE of any of the can is recreated. } #pragma mark-table View data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return 1;}- (Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return
Self.commentsArray.count; }-(UITableViewCell *) TableView: (UITableView *)TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {static nsstring *id=@ "comment"; Commentcell *cell=[
TableView Dequeuereusablecellwithidentifier:id];
if (cell==nil) {Cell=[[commentcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:id];}
Cell.commentdata=self.commentsarray[indexpath.row];
return cell; }-(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;} return width;  } @end

Code for specific custom cell

#import <UIKit/UIKit.h> @class Getcommentdata;
@interface Commentcell:uitableviewcell @property (nonatomic,strong) Getcommentdata *commentdata;
@property (Nonatomic,strong) Uilabel *namelabel;
@property (Nonatomic,strong) Uilabel *titlelabel;
@property (Nonatomic,strong) Uilabel *datelabel; @end #import "commentCell.h" #import "commentsModel.h" @implementation Commentcell-(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];} return self;
}-(void) setup{_namelabel=[uilabel new];
[Self.contentview Addsubview:_namelabel];
_namelabel.textcolor=[uicolor colorwithred:0.891 green:0.549 blue:0.073 alpha:1.000]; _namelabel.font=[uifont systemfontofsize: 15];
_namelabel.numberoflines=1;
_titlelabel=[uilabel new];
[Self.contentview Addsubview:_titlelabel];
_titlelabel.textcolor=[uicolor Darkgraycolor];
_titlelabel.font=[uifont systemfontofsize:15];
_titlelabel.numberoflines=0;
_datelabel=[uilabel new];
[Self.contentview Addsubview:_datelabel];
_datelabel.textcolor=[uicolor colorwithred:0.679 green:0.166 blue:0.828 alpha:1.000];
_datelabel.font=[uifont systemfontofsize:15];
_datelabel.numberoflines=1;
CGFloat margin=10;
UIView *contentview=self.contentview; _namelabel.sd_layout. Leftspacetoview (Contentview,margin). Topspacetoview (Contentview,margin). RightSpaceToView (
Contentview,margin). Heightis (20); _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 (
150); [Self Setupautoheightwithbottomviewsarray:@[_titlelabel,_datelabel,_namelabEl] Bottommargin:margin]; }-(void) awakefromnib {}-(void) setselected: (BOOL) selected animated: (bool) animated {[Super Setselected:selected Anima
Ted:animated];  } @end

Related Article

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.