IOS 自定义每行高度不同的UITableView

视图控制器 头文件  #WLViewController.h

#import <UIKit/UIKit.h>
@interface WLViewController : UITableViewController
@end

视图控制器 实现部分  #WLViewController.m

#import "WLViewController.h"
#import "WLStatusFrame.h"
#import "WLStatusCell.h"
#import "WLStatus.h"

@interface WLViewController ()

/**
 * frame模型
 */
@property(nonatomic,strong) NSArray *statusFrame;

@end

@implementation WLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}


/**
 * 获取组中行数
 */
-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.statusFrame.count;
}

/**
 * 获取cell
 */
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   //创建cell
    WLStatusCell *cell=[WLStatusCell cellWithTableView:tableView];
    
   //通过模型为cell进行初始化
   cell.statusFrame=self.statusFrame[indexPath.row];
    
   //返回cell
   return cell;
}

/**
 * 设置每行 tableView的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WLStatusFrame *statusFrame=self.statusFrame[indexPath.row];
    return statusFrame.cellHeight;
    
}

/**
 * 通过懒加载 将数据封装成模型
 */
-(NSArray *)statusFrame{
    if(_statusFrame==nil){
        NSString *path=[[NSBundle mainBundle]pathForResource:@"statuses" ofType:@"plist"];
        NSArray *plistData=[NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *statusFrameArray=[[NSMutableArray alloc]init];
        for (NSDictionary *item in plistData) {
            
            //创建数据模型
            WLStatus *status=[WLStatus statusWithDict:item];
            
            //创建frame模型 并将数据模型保存在frame模型中
            WLStatusFrame *statusFrame=[[WLStatusFrame alloc]init];
            statusFrame.status=status;
            
            //保存frame模型
            [statusFrameArray addObject:statusFrame];
        }
        _statusFrame=statusFrameArray;
    }
    return _statusFrame;
}
@end

自定义cell 类 头文件 #MJStatusCell.h 

#import <UIKit/UIKit.h>
@class MJStatusFrame;

@interface MJStatusCell : UITableViewCell
@property (nonatomic, strong) MJStatusFrame *statusFrame;

+ (instancetype)cellWithTableView:(UITableView *)tableView;

@end

自定义cell 类实现 #MJStatusCell.m

#define WLNameFont  [UIFont systemFontOfSize:14]
#define WLTextFont  [UIFont systemFontOfSize:14]
#import "WLStatusCell.h"
#import "WLStatusFrame.h"
#import "WLStatus.h"

@interface WLStatusCell()

@property(nonatomic,weak)UIImageView *iconView;
@property(nonatomic,weak)UILabel *nameView;
@property(nonatomic,weak)UILabel *textView;
@property(nonatomic,weak)UIImageView *vipView;
@property(nonatomic,weak)UIImageView *pictureView;

@end

@implementation WLStatusCell

/**
 * 创建并返回一个WLStatusCell
 */
+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID=@"status";
    WLStatusCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell=[[WLStatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
 
    
}

/**
 * 重写初始化cell方法 
 * 创建所有子控件
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //头像
        UIImageView *iconView=[[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView=iconView;
        
        //名字
        UILabel *nameView=[[UILabel alloc]init];
        [self.contentView addSubview:nameView];
        nameView.font=WLNameFont;
        self.nameView=nameView;
        
        //文字内容
        UILabel *textView=[[UILabel alloc]init];
        [self.contentView addSubview: textView];
        textView.font=WLTextFont;
        textView.numberOfLines=0;
        self.textView=textView;
        
        //Vip
        UIImageView *vipView=[[UIImageView alloc]init];
        vipView.image=[UIImage imageNamed:@"vip"];
        [self.contentView addSubview:vipView];
        self.vipView=vipView;
        
        //文章配图
        UIImageView *pictureView=[[UIImageView alloc]init];
        [self.contentView addSubview:pictureView];
        self.pictureView=pictureView;

    }
    return self;
}

/*
 * 覆盖属性statusFrame的 set方法
 * 当为statusFrame属性传值时会自动调用该方法
 */
-(void)setStatusFrame:(WLStatusFrame *)statusFrame{
    
    //保存数据模型
    _statusFrame=statusFrame;
    
    //设置frame
    [self settingFrame];
    
    //设置数据
    [self settingData];
    
}

/*
 * 设置子空间frame
 */
-(void)settingFrame{
    WLStatus *status=self.statusFrame.status;
    //设置名称frame
    self.nameView.frame=self.statusFrame.nameFrame;
    
    //设置内容frame
    self.textView.frame=self.statusFrame.textFrame;
    
    //设置头像frame
    self.iconView.frame=self.statusFrame.iconFrame;
    
    //设置vip图标frame
    self.vipView.frame=self.statusFrame.vipFrame;
    
    //设置配图frame
    if(status.picture){
        self.pictureView.frame=self.statusFrame.pictureFrame;
    }
}

/*
 * 设置子控件的数据
 */
-(void)settingData{

    WLStatus *status=self.statusFrame.status;
    
    //头像
    self.iconView.image=[UIImage imageNamed:status.icon];
    
    //名称
    self.nameView.text=status.name;
    
    //内容
    self.textView.text=status.text;

    //vip图标
    if(status.vip){
        self.vipView.hidden=NO;
    }else{
        self.vipView.hidden=YES;
    }
    
    //配图
    if(status.picture){
       self.pictureView.hidden=NO;
       self.pictureView.image=[UIImage imageNamed:status.picture];
    }else{
       self.pictureView.hidden=YES;
    }

}

@end

frame数据模型头文件  WLStatusFrame.h

#import <Foundation/Foundation.h>

@class WLStatus;
@interface WLStatusFrame : NSObject
/**
 *内容的frame值
 */
@property(nonatomic,assign,readonly)CGRect textFrame;

/**
 *名字的frame值
 */
@property(nonatomic,assign,readonly)CGRect nameFrame;

/**
 *头像的frame值
 */
@property(nonatomic,assign,readonly)CGRect iconFrame;

/**
 *图片的frame值
 */
@property(nonatomic,assign,readonly)CGRect pictureFrame;

/**
 *vip的frame值
 */
@property(nonatomic,assign,readonly)CGRect vipFrame;

/**
 *cell的高度
 */
@property(nonatomic,assign,readonly)CGFloat cellHeight;

/**
 *保存 statusModel
 */
@property(nonatomic,strong)WLStatus *status;

@end

farme数据模型实现   WLStatusFrame.m

#define WLNameFont  [UIFont systemFontOfSize:14]
#define WLTextFont  [UIFont systemFontOfSize:14]
#import "WLStatusFrame.h"
#import "WLStatus.h"

@implementation WLStatusFrame

/**
 * 属性status的set方法
 */
-(void)setStatus:(WLStatus *)status{
    
    //保存数据模型
    _status=status;
    
    //设置组件frame
    [self setFrame];
    
}

/*
 * 根据模型数据计算出frame模型数据 并保存到属性中
 */
-(void)setFrame{
    
    WLStatus *status=self.status;
    CGFloat padding=10; //间距
    
    //头像Frame
    CGFloat icon_x=padding;
    CGFloat icon_y=padding;
    CGFloat icon_w=30;
    CGFloat icon_h=30;
    _iconFrame=CGRectMake(icon_x, icon_y, icon_w, icon_h);
    
    //名称Frame
    CGSize nameSize=[self sizeWithContent:status.name maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) fontSize:WLNameFont];
    CGFloat name_x=CGRectGetMaxX(_iconFrame)+padding;
    CGFloat name_y=(icon_h-nameSize.height)/2+padding;
    _nameFrame=CGRectMake(name_x, name_y, nameSize.width, nameSize.height);
    
    //VIP图标Frame
    CGFloat vip_w=14;
    CGFloat vip_h=14;
    CGFloat vip_x=CGRectGetMaxX(_nameFrame)+padding;
    CGFloat vip_y=(icon_h-vip_h)/2+padding;
    _vipFrame=CGRectMake(vip_x, vip_y, vip_w, vip_h);
    
    //内容Frame
    CGFloat text_x=padding;
    CGFloat text_y=CGRectGetMaxY(_iconFrame)+padding;
    CGFloat text_w=300;
    CGSize textSize=[self sizeWithContent:self.status.text maxSize:CGSizeMake(320, MAXFLOAT) fontSize:WLTextFont];
    _textFrame=CGRectMake(text_x, text_y, text_w, textSize.height);
    
    //配图Frame
    CGFloat picture_w=200;
    CGFloat picture_h=200;
    CGFloat picture_x=padding;
    CGFloat picture_y=CGRectGetMaxY(_textFrame)+padding;
    _pictureFrame=CGRectMake(picture_x, picture_y, picture_w, picture_h);
    
    //ceil高度
    CGFloat cellHeight=0;
    if (status.picture) {
        cellHeight=CGRectGetMaxY(_pictureFrame)+padding;
    }else{
        cellHeight=CGRectGetMaxY(_textFrame)+padding;
    }
    _cellHeight=cellHeight;
}

/*
 *根据传递的字符串 和字体大小来计算 在屏幕上的尺寸
 * sizeByContent @字符串内容
 * maxSize 最大的范围尺寸
 * fontSize  字体的大小
 */
- (CGSize)sizeWithContent:(NSString *)content maxSize:(CGSize)maxSize fontSize:(UIFont *)font{
    NSDictionary *attrs=@{NSFontAttributeName:font}; //设置字体
    CGRect rect= [content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
    return rect.size;
    
}
@end

数据模型头文件  WLStatus.h

#import <Foundation/Foundation.h>

@interface WLStatus : NSObject

@property(nonatomic,copy)NSString * text;
@property(nonatomic,copy)NSString * name;
@property(nonatomic,copy)NSString * icon;
@property(nonatomic,copy)NSString * picture;
@property(nonatomic,copy)NSNumber * vip;


-(instancetype)initWithDict:(NSDictionary *)Dict;

+(instancetype)statusWithDict:(NSDictionary *)Dict;

@end

数据模型实现  WLStatus.m

#import "WLStatus.h"

@implementation WLStatus


-(instancetype)initWithDict:(NSDictionary *)Dict{

    if(self=[super init]){
        [self setValuesForKeysWithDictionary:Dict];
    }
    return self;
}


+(instancetype)statusWithDict:(NSDictionary *)Dict{
    return [[self alloc]initWithDict:Dict];
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值