自定义cell自适应高度(17-08-14)

2017-08-14  本文已影响0人  Hilarylii
//
//  ViewController.m
//  UI07_cell自适应高度
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"
#import "ModelCell.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSArray *modelArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self getData];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}
//获取数据
- (void)getData {
    Model *model1 = [[Model alloc] init];
    model1.imageName = @"1.png";
    model1.imageDescription = @"我是小黄人哇哈哈哈哈哈哈哈哈哈";
    Model *model2 = [[Model alloc] init];
    model2.imageName = @"3.JPG";
    model2.imageDescription = @"那个baby啦啦啦啦啦啦";
    
    self.modelArray = @[model1,model2];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model *model = self.modelArray[indexPath.row];
    CGSize imageSize = [UIImage imageNamed:model.imageName].size;
    CGFloat scale = tableView.bounds.size.width / imageSize.width;
    CGFloat imageHeight = imageSize.height * scale;
    CGRect rect = [model.imageDescription boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, 1000.f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20.f]} context:nil];
    
    return imageHeight + rect.size.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    Model *model = self.modelArray[indexPath.row];
    static NSString *modelIdentifier = @"model";
    ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:modelIdentifier];
    if (nil == cell) {
        cell = [[ModelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:modelIdentifier];
    }
    cell.model = model;
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//
//  Model.h
//  UI07_cell自适应高度
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Model : NSObject

@property (nonatomic, retain) NSString *imageName;
@property (nonatomic, retain) NSString *imageDescription;

@end

//
//  ModelCell.h
//  UI07_cell自适应高度
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Model.h"

@interface ModelCell : UITableViewCell

@property (nonatomic, retain) Model *model;

@end

//
//  ModelCell.m
//  UI07_cell自适应高度
//
//  Created by lanou3g on 17/8/11.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "ModelCell.h"

@interface ModelCell ()

@property (nonatomic, retain) UIImageView *modelImageView;
@property (nonatomic, retain) UILabel *modelImageDescriptionLabel;

@end

@implementation ModelCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.modelImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.modelImageView.backgroundColor = [UIColor redColor];
        self.modelImageDescriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.modelImageDescriptionLabel.backgroundColor = [UIColor greenColor];
        self.modelImageDescriptionLabel.font = [UIFont systemFontOfSize:20.f];
        self.modelImageDescriptionLabel.numberOfLines = 0;
        [self.contentView addSubview:self.modelImageView];
        [self.contentView addSubview:self.modelImageDescriptionLabel];
    }
    return self;
}

- (void)setModel:(Model *)model {
    if (_model != model) {
        _model = model;
        self.modelImageView.image = [UIImage imageNamed:model.imageName];
        self.modelImageDescriptionLabel.text = model.imageDescription;
    }
}

-(void)layoutSubviews {
    [super layoutSubviews];
    CGSize imageSize = self.modelImageView.image.size;
    CGFloat scale = self.contentView.bounds.size.width / imageSize.width;
    self.modelImageView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, imageSize.height * scale);
    //计算字符串的高度
     // 参数1:文本计算需要给定的尺寸
     // 参数2:计算选项(以行和字体为标准进行计算)
     // 参数3:字符串的属性
     // 参数4:绘制上下文(填nil)
    CGRect rect = [self.model.imageDescription boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 1000.f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSAttachmentAttributeName : [UIFont systemFontOfSize:20.f]} context:nil];
    self.modelImageDescriptionLabel.frame = CGRectMake(0, self.modelImageView.frame.size.height + 10, self.contentView.frame.size.width, rect.size.height);
    
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

上一篇下一篇

猜你喜欢

热点阅读