UITableView-FDTemplateLayoutCell 与Masonry 一起解决不定高度的cell

控制器

//
//  ViewController.m
//  shiyan
//
//  Created by aimee on 16/9/28.
//  Copyright © 2016年 aimee. All rights reserved.
//

#import "ViewController.h"
#import <UITableView+FDTemplateLayoutCell.h>
#import <Masonry.h>
#import "TableViewCell.h"
#import "Model.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    Model *model;
}
/** <#属性名#> */
@property (nonatomic, strong)UITableView *tableView;
@end
static NSString *  cellID = @"cellID";

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.and.left.and.right.mas_equalTo(0);
    }];
    [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:cellID];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    model = [Model new];
    model.name = @"fhaiosefijafgefeawgefagasgsaaf我飞偶哇和规范iOSA货给哦撒谎个撒谎个";
    model.text = @"fesajigejsapgjepa]jgpirasjgoprajgopsagjeowagjosajgeosa]jgeposajgeopasjgepsajgopsajgeopsajgefeawgfeageawgfresagrasgraerasgra";

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = model;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.tableView fd_heightForCellWithIdentifier:cellID cacheByIndexPath:indexPath configuration:^(TableViewCell *cell) {
        cell.model = model;

    }];
}
@end

模型

//
//  Model.h
//  shiyan
//
//  Created by aimee on 16/9/29.
//  Copyright © 2016年 aimee. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Model : NSObject
/** name */
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *des;
@property (nonatomic, copy) NSString *time;

@end

cell

//
//  TableViewCell.m
//  shiyan
//
//  Created by aimee on 16/9/29.
//  Copyright © 2016年 aimee. All rights reserved.
//
#import "TableViewCell.h"
#import <Masonry.h>
#import "Model.h"
@interface TableViewCell()
/** <#属性名#> */
@property (nonatomic, strong)UIView *whiteView;
/** <#属性名#> */
@property (nonatomic, strong)UILabel *name_L;
@property (nonatomic, strong)UILabel *text_L;

@end
@implementation TableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [self creatSubviews];
    }
    return self;
}
- (void)creatSubviews
{
    self.whiteView = [[UIView alloc] init];
    self.whiteView.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.whiteView];
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
#pragma mark - 关键1这个约束一定要有
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];

    self.text_L = [[UILabel alloc] init];
    [self.whiteView addSubview:self.text_L];

    self.text_L.numberOfLines = 0;
    self.text_L.font = [UIFont systemFontOfSize:20];
    [self.text_L mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(10);
        make.height.greaterThanOrEqualTo(@40);
    }];

    self.name_L = [[UILabel alloc] init];
    [self.whiteView addSubview:self.name_L];
    self.name_L.numberOfLines = 0;
    self.name_L.font = [UIFont systemFontOfSize:20];
    [self.name_L mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.text_L.mas_left);
        make.top.mas_equalTo(self.text_L.mas_bottom).offset(50);
        make.right.mas_equalTo(self.text_L.mas_right);
#pragma mark - 关键2这个约束一定要有
        make.bottom.mas_equalTo(self.whiteView.mas_bottom).offset(-10);
    }];

}
- (void)setModel:(Model *)model
{
    _model = model;
    self.text_L.text = model.text;
    self.name_L.text = model.name;
}
@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值