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

来源:互联网 发布:淘宝联盟pc客户端 编辑:程序博客网 时间:2024/05/08 05:35

控制器

////  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;@endstatic 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 0
原创粉丝点击