自适应UITableView的Cell高度问题
Posted 玉思盈蝶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自适应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 - 20, 10000000) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size; self.cellHeight = contentSize.height + 74; } 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(10, 10, 50, 50); CGSize contentSize = [model.desc boundingRectWithSize:CGSizeMake(K_SCREEN_WIDTH - 20, 10000000) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size; _titleLabelFrame = CGRectMake(CGRectGetMaxX(_headImgViewFrame) + 10, 10, K_SCREEN_WIDTH - 50 - 20, 30); _contentlabelFrame = CGRectMake(10, CGRectGetMaxY(_headImgViewFrame) + 10, K_SCREEN_WIDTH - 20, contentSize.height); _cellHeight = CGRectGetMaxY(_contentlabelFrame); } @end
2.使用第三方Masonry自使用行高:
很方便,要求自己把约束要设置完全!!!
以上是关于自适应UITableView的Cell高度问题的主要内容,如果未能解决你的问题,请参考以下文章
iOS UITableView+FDTemplateLayoutCell 配合AutoLayout分分钟教你实现动态高度自适应
iOS UITableViewableViewCell自适应高度