forkingdog 和 masonry 实现自动计算行高

#import "TableViewController.h"
#import "fdModel.h"
#import "fdTableViewCell.h"
#import <MJExtension.h>
#import "UITableView+FDTemplateLayoutCell.h"

@interface TableViewController ()

@property (nonatomic, copy) NSArray *fdArray;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[fdTableViewCell class] forCellReuseIdentifier:@"cell"];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.fdArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    fdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.fdModel = self.fdArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self.tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(fdTableViewCell *cell) {
        cell.fdModel = self.fdArray[indexPath.row];
    }];
}

#pragma mark - lazyLoad
- (NSArray *)fdArray {
    
    if (_fdArray == nil) {
        _fdArray = [[NSArray alloc] init];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"microblog" ofType:@"plist"];
        _fdArray = [fdModel objectArrayWithFile:filePath];
    }
    return _fdArray;
}

 

cell  布局

#import "fdTableViewCell.h"
#import "fdModel.h"
#import <Masonry.h>

@interface fdTableViewCell ()

@property (nonatomic, strong) UIView *publicView;

@property (nonatomic, strong) UILabel *textLab;

@property (nonatomic, strong) UILabel *nameLable;

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UIImageView *vipView;

@property (nonatomic, strong) UIImageView *picView;

@end

@implementation fdTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createView];
        [self setViewAutoLayout];
    }
    return self;
}

- (void)createView {
    
    self.publicView = [[UIView alloc] init];
    [self.contentView addSubview:self.publicView];
    
    self.iconView = [[UIImageView alloc] init];
    [self.publicView addSubview:self.iconView];
    
    self.nameLable = [[UILabel alloc] init];
    [self.publicView addSubview:self.nameLable];
    
    self.vipView = [[UIImageView alloc] init];
    self.vipView.image = [UIImage imageNamed:@"vip"];
    [self.publicView addSubview:self.vipView];
    
    self.textLab = [[UILabel alloc] init];
    self.textLab.numberOfLines = 0;
    [self.publicView addSubview:self.textLab];
    
    self.picView = [[UIImageView alloc] init];
    [self.publicView addSubview:self.picView];
}

- (void)setViewAutoLayout {
    
    [self.publicView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(@10);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
// 这里是重点。。。 make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10); }]; [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.left.equalTo(@10); make.width.equalTo(@44); make.height.equalTo(@44); }]; [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.left.equalTo(self.iconView.mas_right).with.offset(10); }]; [self.nameLable sizeToFit]; [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.left.equalTo(self.nameLable.mas_right).with.offset(10); make.height.equalTo(@15); make.width.equalTo(@15); }]; [self.textLab mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.iconView.mas_bottom).with.offset(10); make.left.equalTo(self.iconView); make.right.equalTo(@-20); }]; [self.picView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.textLab.mas_bottom).with.offset(10); make.left.equalTo(self.textLab); // 这个是重点。。。 make.bottom.equalTo(self.contentView); }]; } - (void)setFdModel:(fdModel *)fdModel { _fdModel = fdModel; self.iconView.image = [UIImage imageNamed:fdModel.icon]; self.nameLable.text = fdModel.name; self.vipView.hidden = (![fdModel.vip isEqual:@1]); self.textLab.text = fdModel.text; self.picView.image = [fdModel.picture length] > 0 ? [UIImage imageNamed:fdModel.picture] : nil; }

 

posted @ 2015-10-22 17:24  杨智帆  阅读(403)  评论(0编辑  收藏  举报