
正文
自适应UITableView的Cell高度问题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.自己计算Cell的高度返回:
1>model中计算:
//
// InfoModel.h
// OCDemo
//
// Created by 思 彭 on 16/12/27.
// Copyright © 2016年 思 彭. All rights reserved.
// #import <Foundation/Foundation.h> @interface InfoModel : NSObject @property (nonatomic, copy) NSString *uid;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *headImage; @property (nonatomic, assign) CGFloat cellHeight; - (instancetype)initWithDictionary: (NSDictionary *)dic; @end
//
// InfoModel.m
// OCDemo
//
// Created by 思 彭 on 16/12/27.
// Copyright © 2016年 思 彭. All rights reserved.
// #import "InfoModel.h" @implementation InfoModel - (instancetype)initWithDictionary: (NSDictionary *)dic { if (self = [super init]) { self.title = dic[@"title"];
self.headImage = dic[@"img"];
self.desc = dic[@"desc"]; CGSize contentSize = [self.desc boundingRectWithSize:CGSizeMake(K_SCREEN_WIDTH - , ) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:]} context:nil].size;
self.cellHeight = contentSize.height + ;
}
return self;
} @end
2.自定义CellFrame类存储Cell高度:
//
// CellFrame.h
// OCDemo
//
// Created by 思 彭 on 16/12/28.
// Copyright © 2016年 思 彭. All rights reserved.
// #import <Foundation/Foundation.h>
#import "InfoModel.h" @interface CellFrame : NSObject @property (nonatomic, assign) CGRect headImgViewFrame;
@property (nonatomic, assign) CGRect titleLabelFrame;
@property (nonatomic, assign) CGRect contentlabelFrame;
@property (nonatomic, assign) CGFloat cellHeight; @property (nonatomic, strong) InfoModel *model; @end
//
// CellFrame.m
// OCDemo
//
// Created by 思 彭 on 16/12/28.
// Copyright © 2016年 思 彭. All rights reserved.
// #import "CellFrame.h" @implementation CellFrame - (void)setModel:(InfoModel *)model { _model = model;
_headImgViewFrame = CGRectMake(, , , );
CGSize contentSize = [model.desc boundingRectWithSize:CGSizeMake(K_SCREEN_WIDTH - , ) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:]} context:nil].size;
_titleLabelFrame = CGRectMake(CGRectGetMaxX(_headImgViewFrame) + , , K_SCREEN_WIDTH - - , );
_contentlabelFrame = CGRectMake(, CGRectGetMaxY(_headImgViewFrame) + , K_SCREEN_WIDTH - , contentSize.height);
_cellHeight = CGRectGetMaxY(_contentlabelFrame);
} @end
2.使用第三方Masonry自使用行高:
很方便,要求自己把约束要设置完全!!!







