iOS UITableView cell自适应内容高度

定义UITableView 并且遵守两个协议

    firstTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];

    firstTableView.separatorStyle = NO;

    firstTableView.delegate = self;

    firstTableView.dataSource = self;

    [self.view addSubview:firstTableView];


//返回每个cell的高度

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

{

    FirstFrame *firstFrame = [frameArr objectAtIndex:indexPath.row];

    return firstFrame.heightTableViewCellRow;


}


//cell的内容

/*

                FirstData *firstData = [FirstData arrayWithDic:dic];//网络加载json解析为对象

                FirstFrame *firstFrame = [[FirstFrame alloc]init];//计算cell高度的累

                firstFrame.firstData = firstData;

                

                [firstDataArr addObject:firstData];//数据类数组

                [frameArr addObject:firstFrame];//cell高度类数组

*/

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

{

    FirstTableViewCell *cell = [FirstTableViewCell theCorpusWithTableView:tableView];//自定义cell

    cell.firstFrame = [frameArr objectAtIndex:indexPath.row];

    cell.firstData = [firstDataArr objectAtIndex:indexPath.row];

    return cell;

}


#import "FirstTableViewCell.h"

#import "FirstFrame.h"

#import "FirstData.h"

@implementation FirstTableViewCell

+(instancetype)theCorpusWithTableView:(UITableView *)tableView

{

    static NSString *cellID = @"cellid";

    FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {

        cell = [[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;

}

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

{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        _lineView = [[UIView alloc]init];

        [self.contentView addSubview:_lineView];

        

        _timeBackView = [[UIView alloc]initWithFrame:CGRectMake(12, 15, 120, 17)];

        [self.contentView addSubview:_timeBackView];

  

        _titleLabel = [[UILabel alloc]init];

        [self.contentView addSubview:_titleLabel];

       

        

        _faceIageView = [[UIImageView alloc]init];

       [self.contentView addSubview:_faceIageView];

       

    

    }

    return self;

}

/*。 重写set方法

    cell.firstFrame = [frameArr objectAtIndex:indexPath.row];

    cell.firstData = [firstDataArr objectAtIndex:indexPath.row];

*/

-(void)setFirstData:(FirstData *)firstData

{

    _firstData = firstData;

    _timeLabel.text = _firstData.createtime;

    _titleLabel.text = _firstData.title;


}

-(void)setFirstFrame:(FirstFrame *)firstFrame

{

    _firstFrame = firstFrame;

    //将计算的控件frame赋值

    _lineView.frame = _firstFrame.lineViewFrame;

    _titleLabel.frame = _firstFrame.titleLabelFrame;

    _faceIageView.frame = _firstFrame.faceIageViewFrame;

}


#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@class FirstData;

@interface FirstFrame : NSObject

@property (nonatomic,strong) FirstData *firstData;


@property (nonatomic,assign,readonly) CGRect lineViewFrame;

@property (nonatomic,assign,readonly) CGRect titleLabelFrame;

@property (nonatomic,assign,readonly) CGRect faceIageViewFrame;


@property (nonatomic,assign) CGFloat heightTableViewCellRow;//cell高度




@end


#import "FirstFrame.h"

#import "FirstData.h"

@implementation FirstFrame

-(void)setFirstData:(FirstData *)firstData//重写set方法

{

    _firstData = firstData;

        CGRect rectname = [firstData.title boundingRectWithSize:CGSizeMake(kScreenWidth-46, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];

        _titleLabelFrame = CGRectMake(34, 47-32, kScreenWidth-46, rectname.size.height);

        _lineViewFrame = CGRectMake(16, 0, 1, 60+rectname.size.height+(kScreenWidth-46)*32/66.0-32);

        _faceIageViewFrame = CGRectMake(34, 59 +rectname.size.height-32 , kScreenWidth-46, (kScreenWidth-46)*32/66.0);


        //cell高度

        _heightTableViewCellRow = 60+rectname.size.height+(kScreenWidth-46)*32/66.0-32;


    

}

@end




//不明白的欢迎私聊,指正














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值