首页 > 代码库 > 自适应UITableView的Cell高度问题

自适应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高度问题