美文网首页
Masonry实现TableViewCell高度自适应

Masonry实现TableViewCell高度自适应

作者: 我想走走 | 来源:发表于2016-12-05 15:44 被阅读116次

我想说写这个瞬间感觉好累啊。。。
先看下效果。


2016-12-05 15_42_14.gif

ViewController.m内容

//
//  ViewController.m
//  TableViewCellAuto
//
//  Created by mibo02 on 16/12/5.
//  Copyright © 2016年 mibo02. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import "TestTableViewCell.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    for (NSUInteger i = 0; i<10; ++i) {
        TestModel *model = [[TestModel alloc] init];
        model.title = @"这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,这个是测试的标题,";
        model.desc = @"这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。这个是内容。";
        
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}
- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    
    return _dataSource;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CellIdentifier";
    
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        cell = [[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.indexPath = indexPath;
    cell.block = ^(NSIndexPath *path) {
        //刷新这一行,当进行点击的时候
        [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
    };
    TestModel *model = [self.dataSource objectAtIndex:indexPath.row];
    [cell configCellWithModel:model];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    return [TestTableViewCell heightWithModel:model];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

TestTableViewCell.h的内容

//
//  TestTableViewCell.h
//  TableViewCellAuto
//
//  Created by mibo02 on 16/12/5.
//  Copyright © 2016年 mibo02. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TestModel : NSObject

@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *desc;
@property (nonatomic, assign)BOOL isExpanded;

@end

typedef void (^TestBlock)(NSIndexPath *indexPath);

@interface TestTableViewCell : UITableViewCell
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *descLabel;
@property (nonatomic, strong)NSIndexPath *indexPath;
@property (nonatomic, copy) TestBlock block;
- (void)configCellWithModel:(TestModel *)model;
+(CGFloat)heightWithModel:(TestModel *)model;




@end

TestTableViewCell.m内容

//
//  TestTableViewCell.m
//  TableViewCellAuto
//
//  Created by mibo02 on 16/12/5.
//  Copyright © 2016年 mibo02. All rights reserved.
//

#import "TestTableViewCell.h"
#import "Masonry.h"


@interface TestTableViewCell ()
@property (nonatomic, strong)UILabel *lineLabel;
@property (nonatomic, strong)TestModel *model;

@end



@implementation TestTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor greenColor];
        imageView.layer.cornerRadius = 35;
        imageView.layer.borderColor = [UIColor redColor].CGColor;
        imageView.layer.borderWidth = 2;
        [self.contentView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.top.mas_equalTo(15);
        }];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        //
        // 兼容6.0
        self.titleLabel.preferredMaxLayoutWidth = w - 100 - 15;
          [self.contentView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).offset(15);
            make.right.mas_equalTo(self.contentView).offset(-15);
            make.top.mas_equalTo(imageView);
        }];
        self.descLabel = [UILabel new];
        self.descLabel.textColor =[UIColor blueColor];
        self.descLabel.font = [UIFont systemFontOfSize:13];
        self.descLabel.numberOfLines = 0;
        self.descLabel.textAlignment = NSTextAlignmentLeft;
        
        //
        self.descLabel.preferredMaxLayoutWidth = w - 100 - 45;
        [self.contentView addSubview:self.descLabel];
        [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).offset(15);
            make.right.mas_equalTo(self.contentView).offset(-15);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
        }];
        self.descLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
        [self.descLabel addGestureRecognizer:tap];
        
        UILabel *lineLabel = [[UILabel alloc] init];
        lineLabel.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:lineLabel];
        [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.descLabel.mas_bottom).offset(19);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(1);
        }];
        self.lineLabel = lineLabel;
    }
    return self;
}

- (void)configCellWithModel:(TestModel *)model
{
    self.model = model;
    self.titleLabel.text = model.title;
    self.descLabel.text = model.desc;
    if (model.isExpanded) {
        [self.descLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.right.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
        }];
    } else {
        [self.descLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.right.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
            make.height.mas_equalTo(40);
        }];
    }
}

- (void)onTap {
    self.model.isExpanded = !self.model.isExpanded;
    if (self.block) {
        self.block(self.indexPath);
    }
    [self configCellWithModel:self.model];
    [self.contentView setNeedsUpdateConstraints];
    [self.contentView updateConstraintsIfNeeded];
    [UIView animateWithDuration:0.15 animations:^{
        [self.contentView layoutIfNeeded];
    }];
}
+(CGFloat)heightWithModel:(TestModel *)model
{
    TestTableViewCell *cell = [[TestTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@""];
    [cell configCellWithModel:model];
    [cell layoutIfNeeded];
    CGRect frame = cell.descLabel.frame;
    return frame.origin.y + frame.size.height + 20;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


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

    // Configure the view for the selected state
}

@end

@implementation TestModel



@end

大家可以尝试一下。感觉挺好用的。么么哒。

相关文章

网友评论

      本文标题:Masonry实现TableViewCell高度自适应

      本文链接:https://www.haomeiwen.com/subject/arakmttx.html