UITableView自动计算高度

128 阅读1分钟
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface AutoCell : UITableViewCell

- (void)configWithTexts:(NSArray <NSString *> *)cellTexts;

@end

NS_ASSUME_NONNULL_END
///======================================================
#import "AutoCell.h"
#import <Masonry/Masonry.h>

@interface AutoCell ()

@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *middleLabel;
@property (nonatomic, strong) UILabel *rightLabel;

@end

@implementation AutoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.leftLabel];
        [self.contentView addSubview:self.middleLabel];
        [self.contentView addSubview:self.rightLabel];
        [self p_addMasonry];
    }
    return self;
}

- (void)configWithTexts:(NSArray<NSString *> *)cellTexts {
    // Check
    assert(cellTexts.count == 3);
    
    self.leftLabel.text = cellTexts[0];
    self.middleLabel.text = cellTexts[1];
    self.rightLabel.text = cellTexts[2];
}

#pragma mark - # Private Methods
- (void)p_addMasonry {
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left);
        make.bottom.mas_lessThanOrEqualTo(self.contentView.mas_bottom);
    }];
    
    [self.middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.bottom.mas_lessThanOrEqualTo(self.contentView.mas_bottom);
    }];
    
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.bottom.mas_lessThanOrEqualTo(self.contentView.mas_bottom);
    }];
}

#pragma mark - # Getter
- (UILabel *)leftLabel {
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.textColor = [UIColor grayColor];
        _leftLabel.font = [UIFont systemFontOfSize:14];
        _leftLabel.layer.masksToBounds = YES;
        _leftLabel.layer.borderWidth = 0.5f;
        _leftLabel.layer.borderColor = [UIColor brownColor].CGColor;
        _leftLabel.numberOfLines = 0;
    }
    return _leftLabel;
}

- (UILabel *)middleLabel {
    if (!_middleLabel) {
        _middleLabel = [[UILabel alloc] init];
        _middleLabel.textColor = [UIColor grayColor];
        _middleLabel.font = [UIFont systemFontOfSize:14];
        _middleLabel.layer.masksToBounds = YES;
        _middleLabel.layer.borderWidth = 0.5f;
        _middleLabel.layer.borderColor = [UIColor brownColor].CGColor;
        _middleLabel.numberOfLines = 0;
    }
    return _middleLabel;
}

- (UILabel *)rightLabel {
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.textColor = [UIColor grayColor];
        _rightLabel.font = [UIFont systemFontOfSize:14];
        _rightLabel.layer.masksToBounds = YES;
        _rightLabel.layer.borderWidth = 0.5f;
        _rightLabel.layer.borderColor = [UIColor brownColor].CGColor;
        _rightLabel.numberOfLines = 0;
    }
    return _rightLabel;
}

@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
///======================================================
#import "ViewController.h"
#import "AutoCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <NSArray <NSString *> *> *dataSource;

@end

@implementation ViewController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(loadData)];
    [self.view addSubview:self.tableView];
    [self loadData];
}

- (void)loadData {
    [self.dataSource removeAllObjects];
    // 生成数据
    NSString *string = @"Masonry is a light-weight layout framework which wraps AutoLayout with a nicer syntax.";
    for (NSInteger i = 0; i < 200; i++) {
        NSMutableArray *textArray = [NSMutableArray new];
        for (NSInteger j = 0; j < 3; j++) {
            [textArray addObject:[string substringToIndex:arc4random_uniform((uint32_t)string.length)]];
        }
        [self.dataSource addObject:textArray];
    }
    //刷新
    [self.tableView reloadData];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AutoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AutoCell class]) forIndexPath:indexPath];
    [cell configWithTexts:self.dataSource[indexPath.row]];
    return cell;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 80;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        [_tableView registerClass:[AutoCell class] forCellReuseIdentifier:NSStringFromClass([AutoCell class])];
    }
    return _tableView;
}

@end