IOS 之纯代码计算cell的搞定(文字+图片)

  1.自从换了工作之后,好久都没有碰自己的博客园了,今天刚好学到这个知识点就想起来写点东西了呀,tableview的使用在我们的应用的十分广泛,我特地的学习了下纯代码计算的cell的高度,在计算的中遇到了几个问题,但是还解决了,我直接上代码啦!!使用的是mvc的框架来实现的高度的计算,这里是自己的写的数据,不是接口的数据。用来测试了!!

2.项目中包括下面几个类:(model,Frame, tableviewcell , controller)这四个类 :(纯代码计算高Demo来说明下)

3.Model类(Home)

Home.h的代码:

    @property (nonatomic,assign)CGFloat cellHeight;

     @property (nonatomic,strong)NSString *iconImageview; 

     @property (nonatomic,strong)NSString *name;

     @property (nonatomic,strong)NSString *time;

     @property (nonatomic,strong)NSString *content;

     @property (nonatomic,strong)NSString *imagePath;

     -(instancetype)initWithUser:(NSString *)iconImage name:(NSString *)name time:(NSString *)time content:(NSString *)content imagepath:(NSString *)imagePath;

    -(instancetype)initWithUser:(NSString *)iconImage name:(NSString *)name tiem:(NSString *)time content:(NSString *)content;

Home.m的代码:

  -(instancetype)initWithUser:(NSString *)iconImage name:(NSString *)name time:(NSString *)time content:(NSString *)content imagepath:(NSString *)imagePath{

 

 

 

    self = [super init];

 

    if (self) {

 

        self.iconImageview = iconImage;

 

        self.name = name;

 

        self.time = time;

 

        self.content = content;

 

        self.imagePath = imagePath;

 

    }

 

    return self;

 

}

 

 

 

-(instancetype)initWithUser:(NSString *)iconImage name:(NSString *)name tiem:(NSString *)time content:(NSString *)content{

 

 

 

    self = [super init];

 

    

 

    if (self) {

 

        

 

        self.iconImageview = iconImage;

 

        self.name = name;

 

        self.time = time;

 

        self.content = content;

 

        

 

    }

 

    return  self;

 

}

4.Frame类:(HomeFrame)在这里frame里面得到cellHeight 的高度

HomeFrame.h

 

  @class Home;

 

  @interface HomeFrame : NSObject

 

  @property (nonatomic,strong)Home *home;

 

  @property (nonatomic,assign)CGFloat cellHeight;

 

  @property (nonatomic,assign)CGRect iconFrame;

 

  @property (nonatomic,assign)CGRect nameFrame;

 

  @property (nonatomic,assign)CGRect tiemFreme;

 

  @property (nonatomic,assign)CGRect contentFrame;

 

  @property (nonatomic,assign)CGRect pictureFrame;

HomeFrame.m

    

#define SCREENW   [UIScreen mainScreen].bounds.size.width

#import "HomeFrame.h"

#import "Home.h"

 

@implementation HomeFrame

 

-(void)setHome:(Home *)home{

 

    _home = home;

    // cell的宽度

    //计算iconframe

    CGFloat iconX = 10;

    CGFloat iconY = 10;

    CGFloat iconW = 40;

    CGFloat iconH = 40;

    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);

    //nameFrme

    CGSize maxSize = CGSizeMake(SCREENW-10, MAXFLOAT);

    CGSize nameSize = [home.name boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil].size;

    CGFloat nameX = CGRectGetMaxX(_iconFrame) + 10;

    CGFloat nameY = iconY + (iconH-nameSize.height)/2;

    _nameFrame = (CGRect){{nameX,nameY},nameSize};

    

    //timeFrame

    CGSize timeSize = [home.time boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil].size;

    CGFloat timeX = CGRectGetMaxX(_nameFrame)+20;

    CGFloat timeY = iconY +(iconH - timeSize.height)/2;

    _tiemFreme = (CGRect){{timeX,timeY},timeSize};

 

    //contentFrame

    // 设置内容

    CGSize textSize = [home.content boundingRectWithSize:(CGSize){SCREENW-10,MAXFLOAT} options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;

    CGFloat textX = 5;

    CGFloat textY = CGRectGetMaxY(_tiemFreme)+10;

    _contentFrame = (CGRect){{textX,textY},textSize};

    

    

    //图片的存在的问题

    if (home.imagePath.length == 0) {

    

        self.cellHeight = CGRectGetMaxY(_contentFrame)+10;

        return;

        

    }else{

        

    CGFloat  imageW = 100;

    CGFloat  imageH = 100;

    CGFloat  imageX = 10;

    CGFloat  imageY = CGRectGetMaxY(_contentFrame)+10;

    self.pictureFrame = CGRectMake(imageX, imageY, imageW, imageH);

    self.cellHeight = CGRectGetMaxY(_pictureFrame)+10;

    }

}

5.TableviewCell类(TableviewCell)

 TableviewCell.h

   @class HomeFrame;

   @interface TableViewCell : UITableViewCell

   @property (nonatomic,strong)HomeFrame *homeFrame; 

  +(id)cellWithTable:(UITableView *)tableview;

 

 TableviewCell.m

  

#import "TableViewCell.h"

 

#import "Home.h"

 

#import "HomeFrame.h"

 

 

 

@interface TableViewCell()

 

@property (nonatomic,strong)UIImageView *iconImageView;

 

@property (nonatomic,strong)UILabel *nameLabel;

 

@property (nonatomic,strong)UILabel *timeLabel;

 

@property (nonatomic,strong)UILabel *contentLabel;

 

@property (nonatomic,strong)UIImageView *myImageView;

 

@property (nonatomic,strong)Home *home;

 

@end

 

@implementation TableViewCell

 

 

 

 

 

- (instancetype)initWithTableView:(UITableView *)tableView

 

{

 

    TableViewCell *cell = [TableViewCell cellWithTable:tableView];

 

    return cell;

 

}

 

 

 

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

 

    

 

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

 

    if (self) {

 

        

 

        [self setupCell];

 

    }

 

    

 

    return  self;

 

    

 

}

 

 

 

+(id)cellWithTable:(UITableView *)tableview{

 

 

 

    static NSString *CellID = @"CELL";

 

    TableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellID];

 

    if (cell == nil) {

 

        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];

 

    }

 

    return cell;

 

}

 

 

 

-(void)setupCell{

 

 

 

    //icon

 

    UIImageView *iconImagview = [[UIImageView alloc]init];

 

    iconImagview.contentMode =UIViewContentModeScaleAspectFill;

 

    self.iconImageView = iconImagview;

 

    [self.contentView addSubview:iconImagview];

 

    

 

    //name

 

    UILabel *nameLabel = [[UILabel alloc]init];

 

    nameLabel.font = [UIFont systemFontOfSize:14];

 

    self.nameLabel = nameLabel;

 

    [self addSubview:nameLabel];

 

    

 

    //time

 

    UILabel *timelabel = [[UILabel alloc]init];

 

    timelabel.font = [UIFont systemFontOfSize:14];

 

    self.timeLabel = timelabel;

 

    [self.contentView addSubview:timelabel];

 

    

 

    //content

 

    

 

    //name

 

    UILabel *contentlabel = [[UILabel alloc]init];

 

    contentlabel.font = [UIFont systemFontOfSize:14];

 

    contentlabel.numberOfLines = 0;

 

    self.contentLabel = contentlabel;

 

    

 

    [self.contentView addSubview:contentlabel];

 

    

 

    

 

    //picture

 

    UIImageView *pictureImageview = [[UIImageView alloc]init];

 

    pictureImageview.contentMode =UIViewContentModeScaleAspectFill;

 

    self.myImageView = pictureImageview;

 

    [self.contentView addSubview:pictureImageview];

 

}

 

 

 

-(void)setHomeFrame:(HomeFrame *)homeFrame{

 

 

 

    _homeFrame = homeFrame;

 

    

 

    //赋值

 

    self.iconImageView.image = [UIImage imageNamed:homeFrame.home.iconImageview];

 

    self.nameLabel.text = [NSString stringWithFormat:@"%@",homeFrame.home.name];

 

    self.timeLabel.text = [NSString stringWithFormat:@"%@",homeFrame.home.time];

 

    self.contentLabel.text = [NSString stringWithFormat:@"%@",homeFrame.home.content];

 

    self.myImageView.image = [UIImage imageNamed:homeFrame.home.imagePath];

 

    

 

    //设置frame

 

    self.iconImageView.frame = homeFrame.iconFrame;

 

    self.nameLabel.frame = homeFrame.nameFrame;

 

    self.timeLabel.frame = homeFrame.tiemFreme;

 

    self.contentLabel.frame = homeFrame.contentFrame;

 

    self.myImageView.frame = homeFrame.pictureFrame;

 

   }

 

 

 

 7.Controller类:(HomeController)

 这里就不写代码了,这里就是用到用tableview实现cell的实现基础的东西,但是里面有个重要的东西,模型开发中的那个数据是通过HomeFrame里面得到的Home这里个模型,要注意这一点,这个很容易犯错的大家注意了!!!

Demo下载地址:http://pan.baidu.com/s/1o89wTlS

 

 

  

 

 

posted @ 2016-05-12 15:26  朱峰博客  阅读(692)  评论(0编辑  收藏  举报