美文网首页iOS开发学习
iOS自定义不等高cell

iOS自定义不等高cell

作者: BEYOND黄 | 来源:发表于2017-05-29 15:36 被阅读10次

    直接上代码

    ViewController.h

    #import"ViewController.h"

    #import"MJExtension.h"

    #import"Stautes.h"

    #import"TableViewCell.h"

    staticNSString*ID =@"cell";

    @interfaceViewController()

    @property(nonatomic,strong)NSArray*ary;

    @end

    @implementationViewController

    - (NSArray*)ary{

    if(!_ary) {

    _ary=[Stautesmj_objectArrayWithFilename:@"statuses.plist"];

    }

    return_ary;

    }

    - (void)viewDidLoad {

    [superviewDidLoad];

    NSLog(@"%@---%@",_ary,self.ary);

    [self.tableViewregisterClass:[TableViewCellclass]forCellReuseIdentifier:ID];

    }

    - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    returnself.ary.count;

    }

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    TableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:ID];

    cell.stautes=self.ary[indexPath.row];

    returncell;

    }

    - (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    Stautes*s =self.ary[indexPath.row];

    returns.cellHeight;

    }

    @end

    #import

    @classStautes;

    @interfaceTableViewCell :UITableViewCell

    @property(nonatomic,strong)Stautes*stautes;

    @end

    //

    //TableViewCell.m

    //123

    //

    //Created by Apple on 2017/5/29.

    //Copyright © 2017年Apple. All rights reserved.

    //

    #import"TableViewCell.h"

    #import"Stautes.h"

    #define TextFont [UIFont systemFontOfSize:14]

    #define NameFont [UIFont systemFontOfSize:14]

    @interfaceTableViewCell()

    @property(nonatomic,weak)UIImageView *icon;

    @property(nonatomic,weak)UILabel *name;

    @property(nonatomic,weak)UIImageView*vip;

    @property(nonatomic,weak)UILabel*txlabel;

    @property(nonatomic,weak)UIImageView*pic;

    @end

    @implementationTableViewCell

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{

    if(self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {

    UIImageView*icon = [[UIImageViewalloc]init];

    [self.contentViewaddSubview:icon];

    self.icon=icon;

    UIImageView*pic = [[UIImageViewalloc]init];

    [self.contentViewaddSubview:pic];

    self.pic=pic;

    UIImageView*vip = [[UIImageViewalloc]init];

    vip.image=[UIImageimageNamed:@"vip"];

    [self.contentViewaddSubview:vip];

    self.vip=vip;

    UILabel*name = [[UILabelalloc]init];

    [self.contentViewaddSubview:name];

    self.name=name;

    UILabel*text = [[UILabelalloc]init];

    text.font=TextFont;

    text.numberOfLines=0;

    [self.contentViewaddSubview:text];

    self.txlabel=text;

    }

    returnself;

    }

    - (void)layoutSubviews{

    [superlayoutSubviews];

    self.icon.frame=self.stautes.iconFrame;

    self.name.frame=self.stautes.nameFrame;

    self.vip.frame=self.stautes.vipFrame;

    self.txlabel.frame=self.stautes.textFrame;

    self.pic.frame=self.stautes.pictureFrame;

    }

    - (void)setStautes:(Stautes*)stautes{

    _stautes=stautes;

    self.icon.image=[UIImageimageNamed:stautes.icon];

    self.name.text=stautes.name;

    if(stautes.isVip) {

    self.vip.hidden=NO;

    }else{

    self.vip.hidden=YES;

    }

    self.txlabel.text=stautes.text;

    if(stautes.picture) {

    self.pic.hidden=NO;

    self.pic.image=[UIImage imageNamed:stautes.picture];

    }else{

    self.pic.hidden=YES;

    }

    }

    @end

    #import

    #import

    @interfaceStautes :NSObject

    @property(nonatomic,strong)NSString*text;

    @property(nonatomic,strong)NSString*icon;

    @property(nonatomic,strong)NSString*name;

    @property(nonatomic,strong)NSString*picture;

    @property(nonatomic,assign,getter=isVip)BOOLvip;

    /** frame数据***/

    /**图像的frame */

    @property(nonatomic,assign)CGRecticonFrame;

    /**昵称的frame */

    @property(nonatomic,assign)CGRectnameFrame;

    /** vip的frame */

    @property(nonatomic,assign)CGRectvipFrame;

    /**正文frame */

    @property(nonatomic,assign)CGRecttextFrame;

    /**配图的frame */

    @property(nonatomic,assign)CGRectpictureFrame;

    /** cell的高度*/

    @property(nonatomic,assign)CGFloatcellHeight;

    @end

    //

    //Stautes.m

    //123

    //

    //Created by Apple on 2017/5/29.

    //Copyright © 2017年Apple. All rights reserved.

    //

    #import"Stautes.h"

    @implementationStautes

    - (CGFloat)cellHeight

    {

    if(_cellHeight==0) {

    CGFloatspace =10;

    /**图像*/

    CGFloaticonX = space;

    CGFloaticonY = space;

    CGFloaticonWH =30;

    self.iconFrame=CGRectMake(iconX, iconY, iconWH, iconWH);

    /**昵称*/

    CGFloatnameX =CGRectGetMaxX(self.iconFrame) + space;

    CGFloatnameY = iconY;

    NSDictionary*nameAtt =@{NSFontAttributeName: [UIFontsystemFontOfSize:17]};

    //计算昵称文字的尺寸

    CGSizenameSize = [self.namesizeWithAttributes:nameAtt];

    CGFloatnameW = nameSize.width;

    CGFloatnameH = nameSize.height;

    self.nameFrame=CGRectMake(nameX, nameY, nameW, nameH);

    /** vip */

    if(self.isVip) {

    CGFloatvipX =CGRectGetMaxX(self.nameFrame) + space;

    CGFloatvipW =14;

    CGFloatvipH = nameH;

    CGFloatvipY = nameY;

    self.vipFrame=CGRectMake(vipX, vipY, vipW, vipH);

    }

    /**正文*/

    CGFloattextX = iconX;

    CGFloattextY =CGRectGetMaxY(self.iconFrame) + space;

    CGFloattextW = [UIScreenmainScreen].bounds.size.width-2* space;

    NSDictionary*textAtt =@{NSFontAttributeName: [UIFontsystemFontOfSize:14]};

    //最大宽度是textW,高度不限制

    CGSizetextSize =CGSizeMake(textW,MAXFLOAT);

    CGFloattextH = [self.textboundingRectWithSize:textSizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:textAttcontext:nil].size.height;

    self.textFrame=CGRectMake(textX, textY, textW, textH);

    /**配图*/

    if(self.picture) {//有配图

    CGFloatpictureWH =100;

    CGFloatpictureX = iconX;

    CGFloatpictureY =CGRectGetMaxY(self.textFrame) + space;

    self.pictureFrame=CGRectMake(pictureX, pictureY, pictureWH, pictureWH);

    _cellHeight=CGRectGetMaxY(self.pictureFrame) + space;

    }else{

    _cellHeight=CGRectGetMaxY(self.textFrame) + space;

    }

    }

    return_cellHeight;

    }

    @end

    相关文章

      网友评论

        本文标题:iOS自定义不等高cell

        本文链接:https://www.haomeiwen.com/subject/dowkfxtx.html