iOS tableviewCell自适应高度

效果图:


668737-750ba5a509c8035e.png
717EA841-8896-4B94-8D81-6654087CBA37.png

MVC
在controller中
1、创建对象

@property(nonatomic, strong) UITableView *tableView; /**< tableview */
@property(nonatomic, strong) NSArray *array; /**< array */
@property(nonatomic, strong) NSMutableArray *cellModelArr; /**< 放model的数组 */

2、数组赋值
3、将数据源数组中的数据放入model中,将model放入cellModelArr中

在model中
.h

1、@property(nonatomic, copy) NSString *name; /**< name */
@property(nonatomic, copy) NSString *detail; /**< detail */
@property(nonatomic, assign) CGFloat detailLabelHeight; /**< 行高 */
+ (id)buildModelWithDic:(NSDictionary *)dic;

.m
1、

#import "UIView+LabelHeight.h"

注:这个分类是我写的计算label高度的一个方法。
2、KVC赋值

+ (id)buildModelWithDic:(NSDictionary *)dic {
    return [[Model alloc] initWithDic:dic];
}

- (id)initWithDic:(NSDictionary *)dic {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

3、重写detail属性的set方法

- (void)setDetail:(NSString *)detail {
    _detail = nil;
    _detail = detail;
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 16;
    _cellHeight = [UIView getLabelHeightByWidth:width Title:_detail font:[UIFont systemFontOfSize:15]];
}

在这里set的重写方法中,通过拿到detailLabel中的数据(model中的detail)先行计算detailLabel的高度。
4、(计算label高度的category 方法)

+ (CGFloat)getLabelHeightByWidth:(CGFloat)width Title:(NSString *)title font:(UIFont *)font {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
     [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}

在View中
1、自定义创建xibCell

668737-53108c112b268b97.png
170595EC-404D-46CC-B44A-2D1812EAB7E2.png

上面nameLabel 上下左右加约束,高度固定21。 下面detailLabel上左右加约束,高度固定,将高度拖到对应的.m中
2、
.h中
声明model的属性

@property(nonatomic, strong) Model *cellModel; /**< cellModel */

3、
.m中

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *detailHeight;

4、重写model的set方法
将model中计算得到的detailLabel的高度赋值给从xib脱出来的detailHeight

- (void)setCellModel:(Model *)cellModel {
    _cellModel = cellModel;
    self.nameLabel.text = _cellModel.name;
    self.detailLabel.text = _cellModel.detail;
    self.detailHeight.constant = _cellModel.cellHeight;
}

最后在controller中 tableview的代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
    if (!cell) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
        cell = [nibs lastObject];
    }
    cell.cellModel = [_cellModelArr objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model *model = [_cellModelArr objectAtIndex:indexPath.row];
    return model.cellHeight + 30;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.f;
}

这样能基本满足简单的xibCell中label的高度自适应了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值