tableviewcell 自适应高度(模型数据自适应)

首先先来理一下具体步骤:

1、新建一个自定义的tableviewcell类

2、初始化tableviewcell时同时初始化所有子控件<这里可以先设置子控件的一些属性包括字体、颜色等>

3、提供两个模型类

a、一个是我们常用的数据模型

b、一个是frame模型(数据模型+所有子控件的frame+cell的高度)起个名modelFrame

4、在自定义的cell 中应该提供一个frame属性<modelFrame>

a、将modelFrame模型传递给cell

b、cell根据modelFrame模型给子控件设置frame,根据数据模型给子控件设置具体的数据

5、在tableview的数据源方法中返回cell 的高度

上代码

1、新建一个自定义的tableviewcell类

#import <UIKit/UIKit.h>

@class DescriptionModelFrame;

@interface DetailCell : UITableViewCell

@property(nonatomic,strong)DescriptionModelFrame * DesFrame;

@end

实现文件中

#import "DetailCell.h"

#import "DescriptionModel.h"

#import "DescriptionModelFrame.h"

@interface DetailCell()

@property(nonatomic,strong)UILabel * titleLabel;标题

@property(nonatomic,strong)UILabel * desLabel;详细描述


@end

@implementation DetailCell

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

{

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

    if (self) {

        self.selectionStyle = UITableViewCellSelectionStyleNone;//去掉cell 点击效果

        [self initSubControl];

    }

    return self;

}

-(void)initSubControl

{

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

    titleLable.font = [UIFont systemFontOfSize:15*ScreenZoomScaleSix];

    titleLable.backgroundColor = [UIColor whiteColor];

    self.titleLabel = titleLable;

    [self.contentView addSubview:titleLable];

    

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

    desLabel.font = [UIFont systemFontOfSize:15*ScreenZoomScaleSix];

    desLabel.numberOfLines = 0;

    desLabel.textColor = [UIColor lightGrayColor];

    desLabel.backgroundColor = [UIColor whiteColor];

    self.desLabel = desLabel;

    [self.contentView addSubview:desLabel];

    

}

-(void)setDesFrame:(DescriptionModelFrame *)DesFrame

{

    _DesFrame = DesFrame;

    DescriptionModel * model = DesFrame.desModel;

    

    self.titleLabel.frame = DesFrame.titleLabelF;

    self.titleLabel.text = model.attrName;

    self.desLabel.frame = DesFrame.desLabelF;

    self.desLabel.text = model.attrValue;

}

@end


3、提供两个模型类

a、一个是我们常用的数据模型

#import <Foundation/Foundation.h>


@interface DescriptionModel : NSObject

/**

 * 标题

 */

@property(nonatomic,strong)NSString * attrName;

/**

 * 详细描述

 */

@property(nonatomic,strong)NSString * attrValue;

@end

b、一个是frame模型(数据模型+所有子控件的frame+cell的高度)起个名modelFrame

#import <Foundation/Foundation.h>

@class DescriptionModel;

@interface DescriptionModelFrame : NSObject

@property(nonatomic,assign)CGRect titleLabelF; 标题frame

@property(nonatomic,assign)CGRect desLabelF; 详情frame

@property(nonatomic,strong)DescriptionModel * desModel; 数据模型

@property(nonatomic,assign)CGFloat cellH; 单元格的高度

@end

实现文件中

#import "DescriptionModelFrame.h"

#import "DescriptionModel.h"

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

#define LEFTMEGIN 10

@implementation DescriptionModelFrame

-(void)setDesModel:(DescriptionModel *)desModel

{

    _desModel = desModel;

    

    CGFloat cellW = SCREENW;

    CGFloat titleW = cellW-2*10*ScreenZoomScaleSix;

    CGFloat titleH = 15*ScreenZoomScaleSix;

    CGFloat titleX = LEFTMEGIN*ScreenZoomScaleSix;

    CGFloat titleY = 10*ScreenZoomScaleSix;

    self.titleLabelF = CGRectMakeLiu(titleX, titleY, titleW, titleH);

    

    CGSize desSize = [self sizeWithString:desModel.attrValue font:[UIFont systemFontOfSize:15*ScreenZoomScaleSix]];

    CGFloat desX = LEFTMEGIN*ScreenZoomScaleSix;

    CGFloat desY = CGRectGetMaxY(self.titleLabelF)+LEFTMEGIN*ScreenZoomScaleSix;

    self.desLabelF = (CGRect){{desX,desY},desSize};

    

    CGFloat cellH = CGRectGetMaxY(self.desLabelF)+LEFTMEGIN*ScreenZoomScaleSix;

    self.cellH = cellH;

    

}

- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font

{

    CGRect rect = [string boundingRectWithSize:CGSizeMake(SCREENW-10*ScreenZoomScaleSix*2, 8000)                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin

                                    attributes:@{NSFontAttributeName: font}                                       context:nil];

    

    return rect.size;

}

@end


好了几乎可以大功告成了 下来说说在控制器中如何来搞(里边是我的一些数据可以参考)

NSArray * modeleArray = [DescriptionModel objectArrayWithKeyValuesArray:self.dataDic[@"goodsDetail"][@"goodsAttrs"]];

        NSArray * modelFrameArray = [self modelsFramesWithModls:modeleArray];

        self.modelFrameArray = [modelFrameArray mutableCopy];


//数据模型转frame模型

- (NSArray *)modelsFramesWithModls:(NSArray *)models

{

    NSMutableArray *frames = [NSMutableArray array];

    for (DescriptionModel *model in models) {

        DescriptionModelFrame *f = [[DescriptionModelFrame alloc] init];

        f.desModel = model;

        [frames addObject:f];

    }

    return frames;

}


、、在数据源方法中一句代码搞定

// cell传递模型数据

    cell.DesFrame = self.modelFrameArray[indexPath.row];


cell的高度也出来了

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

{

    DescriptionModelFrame *frame = self.modelFrameArray[indexPath.row];

    return frame.cellH;

}

给个图参考一下 我把单元格分割线去掉了


当然如果搞复杂的单元格 思路是对的 只不过 计算的东西多了




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值