Masonry實(shí)現(xiàn)cell自適應(yīng)高度

此處不介紹Masonry的導(dǎo)入及使用,下面是Masonry的github地址

Masonry

下面介紹cell自適應(yīng)高度實(shí)現(xiàn),直接貼代碼,重要部分講解

ListModel.h文件

#import <Foundation/Foundation.h>

@interface ListModel : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *time;

@end

ListModel.m文件

#import "ListModel.h"

@implementation ListModel

@end

ListCell.h文件

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

static NSString *identifier = @"listCell";

@interface ListCell : UITableViewCell

@property (nonatomic, strong) ListModel *model;

@end

ListCell.m文件

#import "ListCell.h"
#import "View+MASAdditions.h"

@interface ListCell()

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *timeLb;

@end

@implementation ListCell

#pragma mark - lazy load
- (UILabel *)titleLb
{
    if (!_titleLb)
    {
     _titleLb = [[UILabel alloc] init];
     _titleLb.numberOfLines = 0;
        [self.contentView addSubview:_titleLb];
    }

    return _titleLb;
}

- (UILabel *)timeLb
{
    if (!_timeLb)
    {
        _timeLb = [[UILabel alloc] init];
        _timeLb.numberOfLines = 0;
        [self.contentView addSubview:_timeLb];
    }

    return _timeLb;
}

#pragma mark - set
- (void)setModel:(ListModel *)model
{
     _model = model;

    self.titleLb.text = _model.title;
    self.timeLb.text = _model.time;

    [_titleLb   mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(10);
    make.right.equalTo(self.contentView).offset(-10);
    }];

    [_timeLb mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(_titleLb.mas_bottom).offset(10);
    make.left.equalTo(self.contentView).offset(10);
    make.right.bottom.equalTo(self.contentView).offset(-10);
    }];
    }

@end

重點(diǎn):

自定義cell時,要設(shè)置cell里最上方控件與cell.contentView上方的距離,最下方控件與cell.contentView下方的距離,各控件間距離

如上,

titleLb與cell.contentView的上方距離為10

timeLb與cell.contentView的下方距離為10

titleLb與timeLb直接距離為10

ViewController.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m文件

#import "ViewController.h"
#import "ListModel.h"
#import "ListCell.h"
#import "View+MASAdditions.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *listTableView;
    NSMutableArray *dataArray;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *titles = @[@"跑步與不跑步的人,在1天、1月甚至1年來看都沒什么了不起的差距;但在每5年來看的時候,就是身體和精神狀態(tài)的巨大分野;等到了10年再看的時候,也許就是一種人生對另一種人生不可企及的鴻溝。",@"讀過的書不會成為過眼煙云,它們會潛在氣質(zhì)里、在談吐上、在胸襟的無涯,當(dāng)然也可能顯露在生活和文字中。",@"而定投和跑步、讀書一樣,都是人生中最簡單卻又最美好的事情。",@"彼得·林奇曾說:投資者如果能夠不為經(jīng)濟(jì)形式焦慮,不看重市場狀況,只是按照固定的計劃進(jìn)行投資,其成績往往好于那些成天研究,試圖預(yù)測市場并據(jù)此買賣"];
dataArray = [NSMutableArray array];

for (int i = 0; i < titles.count; i++)
{
    ListModel *model = [[ListModel alloc] init];
    model.title = titles[i];
    model.time = @"2017-02-09";
    [dataArray addObject:model];
}

    listTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    listTableView.delegate = self;
    listTableView.dataSource = self;
    listTableView.estimatedRowHeight = 60;
    listTableView.rowHeight = UITableViewAutomaticDimension;
    [listTableView registerClass:[ListCell class] forCellReuseIdentifier:identifier];
    [self.view addSubview:listTableView];
    [listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.model = dataArray[indexPath.row];

    return cell;
}

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

@end

重點(diǎn):

設(shè)置估算高度,必須加上,否則失效

listTableView.estimatedRowHeight = 60;

最后附上效果圖

00665x6Xgy1fckah2byq3j30qo1bfagz.jpeg

最后溫馨提醒:cell里面的控件的約束一定要環(huán)環(huán)相扣,環(huán)環(huán)相扣,環(huán)環(huán)相扣,重要的事情說三次,而且cell里面的約束最頂部的控件對cell頂部約束,最底部的控件也要對cell底部約束
make.left.top.equalTo(self.contentView).offset(10);
make.bottom.equalTo(self.contentView).offset(-10);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容