iOS-动态计算cell的高度

动态计算cell的高度

1。有两种方案,在model型里面设置,这样的好处是,直接获取到model的内容,让后通过内容计算cell的高度
在控制器的cellforheight直接获取模型,返回模型高度,这样很直接
2.还有一种方案是,在cell里面,设置model高度,通过重写mode,来计算cell的高度,因为咋cell里面,所以可以得到cell的contentview来得到最后结果;
3.两种方法比较,通过model的话是重写cellheight的set方法来计算具体参考下面的类

1
2
3
4
5
6
7
8
9
10

#pragma mark -代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//取出帖子模型
WordModel * topic = self.topics[indexPath.row];

//返回这个模型对应的帖子的高度
return topic.cellHeight;
}

类文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

#import <UIKit/UIKit.h>
@class Commend;
@interface WordModel : NSObject

/**id */
@property (nonatomic,strong)NSString * ID;
/**名称 */
@property (nonatomic,strong)NSString * name;

/**头像 */
@property (nonatomic,strong)NSString * profile_image;

/**发帖时间 */
@property (nonatomic,strong)NSString * create_time;

/**文字内容 */
@property (nonatomic,strong)NSString * text;

/**顶的数量 */
@property (nonatomic,assign)NSInteger ding;

/**踩得数量 */
@property (nonatomic,assign)NSInteger cai;

/**转发数量 */
@property (nonatomic,assign)NSInteger repost;

/**评论数量 */
@property (nonatomic,assign)NSInteger comment;
//图片的高度
@property (nonatomic,assign)CGFloat height;
//图片的宽度
@property (nonatomic,assign)CGFloat width;
//图片的路径
@property (nonatomic,strong) NSString * image0;//小图
@property (nonatomic,strong) NSString * image1;//大图
@property (nonatomic,strong) NSString * image2;//中图
//额外的辅助属性
@property (nonatomic,assign,readonly)CGFloat cellHeight;
//帖子类型
@property (nonatomic,strong)NSString * type;
//图片控件的frame
@property (nonatomic,assign,readonly)CGRect pictureF;
//声音控件的frame
@property (nonatomic,assign,readonly)CGRect voiceF;
//图片是否太大
@property (nonatomic,assign,getter=isBigPicture)BOOL bigImage;
//图片下载进度
@property (nonatomic,assign)CGFloat pictureProgress;
//***音频
//播放次数
@property (nonatomic,assign)NSInteger playcount;
//播放时长
@property (nonatomic,assign)NSInteger voicetime;
//****视频
//播放时长
@property (nonatomic,assign)NSInteger videotime;
//视频控件的frame
@property (nonatomic,assign,readonly)CGRect videoF;


//额外的辅助属性

/**热门评论 */
@property (nonatomic,strong)Commend * top_cmt;
//状态
//@property (nonatomic,copy)NSString * qzone_uid;

@end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#import "WordModel.h"
#import <MJExtension.h>
#import "User.h"
#import "Commend.h"
@implementation WordModel
//.h中设置了readonly 因此这边要设置
{
CGFloat _cellHeight;
CGRect _pictureF;
}
+(NSDictionary *)mj_replacedKeyFromPropertyName
{
return @{
@"ID":@"id",
@"top_cmt":@"top_cmt[0]",
@"ctime" :@"top_cmt[0]"};
}
- (NSString *)create_time
{
//日期格式化类
//NSString ->> NSDate
NSDateFormatter * fmt = [[NSDateFormatter alloc]init];
//设置日期格式
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//帖子创建时间
NSDate * create = [fmt dateFromString:_create_time];
if (create.isThisYear) {//今年

if (create.isToday) {//今天
NSDateComponents * cmps = [[NSDate date]deltaFrom:create];

if (cmps.hour>= 1) {//时间差距 >= 1小时
return [NSString stringWithFormat:@"%zd小时前",cmps.hour];
}else if (cmps.minute >= 1){ //1小时 > 时间差距 >= 1分钟
return [NSString stringWithFormat:@"%zd分钟前",cmps.minute];
}else{//一分钟 > 时间差距
return @"刚刚";
}
}else if (create.isYesterday){//昨天
fmt.dateFormat = @"昨天 HH:mm:ss";
return [fmt stringFromDate:create];
}else{//其他
fmt.dateFormat = @"MM-dd HH:mm:ss";
return [fmt stringFromDate:create];
}
}else{//非今年
return _create_time;
}
}
+ (NSDictionary *)mj_objectClassInArray
{
return @{@"top_cmt":@"Commend"};
}



- (CGFloat)cellHeight
{
if (!_cellHeight) {

//文字的Y值
CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width -4 *TopicCellMargin, MAXFLOAT);
// CGFloat textH = [topic.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize].height;
//计算文字高度
CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil].size.height;
//文字部分高度
_cellHeight = TopicCellTextY + textH + TopicCellMargin ;
//根据段子的类型计算cell的高度
if ([self.type isEqualToString:@"10"]) {//图片
//图片显示出来的宽度
CGFloat pictureW = maxSize.width;
//图片显示出来的高度
CGFloat pictureH = pictureW * self.height / self.width;
if (pictureH >= TopicCellPicMaxH) {//图片高度过长
pictureH = TopicCellPicBreakH;
self.bigImage = YES;
}
// 计算图片的fame
CGFloat pictureX = TopicCellMargin;
CGFloat pictureY = TopicCellTextY + textH + TopicCellMargin;
_pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
_cellHeight += pictureH + TopicCellMargin;
}else if ([self.type isEqualToString:@"31"])//声音帖子
{
CGFloat voiceX = TopicCellMargin;
CGFloat voiceY = TopicCellTextY + textH + TopicCellMargin;
CGFloat voiceW = maxSize.width;
CGFloat voiceH = voiceW * self.height / self.width;;
_voiceF = CGRectMake(voiceX, voiceY, voiceW,voiceH);
_cellHeight += voiceH + TopicCellMargin;
//底部工具条高度
_cellHeight += TopicCellBottomBarH + TopicCellMargin;
}else if ([self.type isEqualToString:@"41"])//视频帖子
{
CGFloat videoX = TopicCellMargin;
CGFloat videoY = TopicCellTextY + textH + TopicCellMargin;
CGFloat videoW = maxSize.width;
CGFloat videoH = videoW * self.height / self.width;;
_videoF = CGRectMake(videoX, videoY, videoW,videoH);
_cellHeight += videoH + TopicCellMargin;

}
// Commend * cmt = [self.top_cmt firstObject];
if (self.top_cmt) {
NSString * content = [NSString stringWithFormat:@"%@ : %@",self.top_cmt.user.username,self.top_cmt.content];
CGFloat contentH = [content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size.height;
_cellHeight += TopicCellTopCmtTitleH + TopicCellMargin + contentH ;
}
//底部工具条高度
_cellHeight += TopicCellBottomBarH + TopicCellMargin;
}
return _cellHeight;

}
@end