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

来源:互联网 发布:美国非农就业数据9月 编辑:程序博客网 时间:2024/04/29 05:59

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

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 = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

   if (self) {

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

        [selfinitSubControl];

    }

    return self;

}

-(void)initSubControl

{

   UILabel * titleLable = [[UILabelalloc] init];

    titleLable.font = [UIFontsystemFontOfSize:15*ScreenZoomScaleSix];

    titleLable.backgroundColor = [UIColorwhiteColor];

   self.titleLabel = titleLable;

    [self.contentViewaddSubview:titleLable];

    

   UILabel * desLabel = [[UILabelalloc] init];

    desLabel.font = [UIFontsystemFontOfSize:15*ScreenZoomScaleSix];

    desLabel.numberOfLines =0;

    desLabel.textColor = [UIColorlightGrayColor];

    desLabel.backgroundColor = [UIColorwhiteColor];

   self.desLabel = desLabel;

    [self.contentViewaddSubview: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 = [selfsizeWithString:desModel.attrValuefont:[UIFontsystemFontOfSize: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 = [stringboundingRectWithSize:CGSizeMake(SCREENW-10*ScreenZoomScaleSix*2,8000)                                      options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin

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

    

   return rect.size;

}

@end


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

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

       NSArray * modelFrameArray = [selfmodelsFramesWithModls:modeleArray];

       self.modelFrameArray = [modelFrameArraymutableCopy];


//数据模型转frame模型

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

{

    NSMutableArray *frames = [NSMutableArrayarray];

   for (DescriptionModel *modelin models) {

        DescriptionModelFrame *f = [[DescriptionModelFramealloc] init];

        f.desModel = model;

        [framesaddObject: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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 12306号码被注册了怎么办 12306身份证被注册了怎么办 12306被别人注册了怎么办 铁路1236注册名忘记了怎么办 12306手机被别人注册了怎么办 12306注册手机不用了怎么办 到站后火车票掉了怎么办 在手机上买了票怎么办 智行火车票抢不到票怎么办 高铁买票票丢了怎么办 异地恋房费太贵怎么办 高铁票车上丢了怎么办 取了高铁票丢了怎么办 高铁买了学生票没带学生证怎么办 买的学生票超过区间怎么办 买了超过区间的学生票怎么办 火车票大于学生票购买区间怎么办 买了学生票学生证丢了怎么办 取票学生证没带怎么办 学生卡的失磁怎么办 学生证没有充磁买不了学生票怎么办 买学生票不在优惠区间怎么办 火车票学生优惠次数用完怎么办 学生乘火车优惠磁卡丢了怎么办 磁卡锁的卡丢了怎么办 电梯磁卡扣丢了怎么办 买火车票手机号填错了怎么办 买火车票乘客身份核验失败怎么办 火车票不在一个车厢怎么办站票 坐火车丢东西了怎么办 g2坐过站了怎么办 坐火车买近了怎么办 打印的纸质火车票丢失怎么办 格力空调没保修单怎么办 格力空调不兑现保修怎么办 哈空调如果退市怎么办 空调保修单丢了怎么办 海尔空调发票丢了怎么办 格力空调发票丢了怎么办 联程航班第一程延误怎么办 飞机经停10小时怎么办