iOS开发 UITableView高度自适应

一、使用系统约束自动计算

1.布局UITableViewCell

必须保证cell的垂直方向有约束,且 descLabel的 Content Hugging Priority --> Vertical需设置为252(大于titleLabel的默认251)。


image.png
image.png

2.设置tableView

- (void)initTableView {
    // LeoTableViewSystemCell 垂直方向约束必须有
    // 且 descLabel的 Content Hugging Priority --> Vertical需设置为252(大于titleLabel的默认251)
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LeoTableViewSystemCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LeoTableViewSystemCell class])];
    // xib中设置了自动布局,可以不设置预估高度
//    _tableView.estimatedRowHeight = 126;
}

// xib中设置了自动布局,则不需要实现该协议
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"heightForRowAtIndexPath");
//    return 80;
//}

// xib中设置了自动布局,则不需要实现该协议
//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"estimatedHeightForRowAtIndexPath");
//    return 50;
//}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeoTableViewSystemCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LeoTableViewSystemCell class]) forIndexPath:indexPath];
    if (indexPath.row < _dataSource.count) {
        LeoTableViewCellModel *model = _dataSource[indexPath.row];
        [cell setCellModel:model];
    }
    return cell;
}

二、使用三方库FDTemplateLayoutcell

1.布局UITableViewCell

02.png
01.png

2.

- (void)configureCell:(LeoTableViewTemplateCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    if (indexPath.row < _dataSource.count) {
        LeoTableViewCellModel *model = _dataSource[indexPath.row];
        [cell setCellModel:model];
    }
}

- (void)initTableView {
    // LeoTableViewTemplateCell 垂直方向约束必须有
    // 需要设置descLabel高度能自适应,无约束报错。
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LeoTableViewTemplateCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LeoTableViewTemplateCell class])];
}

// 通过`FDTemplateLayoutcell`计算高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = [tableView fd_heightForCellWithIdentifier:NSStringFromClass([LeoTableViewTemplateCell class]) cacheByIndexPath:indexPath configuration:^(LeoTableViewTemplateCell *cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];
    return height;
}

//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"estimatedHeightForRowAtIndexPath");
//    return 50;
//}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeoTableViewTemplateCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LeoTableViewTemplateCell class]) forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

你可能感兴趣的:(iOS开发 UITableView高度自适应)