UITableView的Cell根据内容计算高度

在日常的开发中我们经常遇到UITableView的cell根据里边的内容动态计算其高度,其中动态计算其高度的方法有很多,在这里我将先介绍其中的一种方法,这种方法的应用比较简单方便,而且可以更快的实现cell的高度自适应

创建UITableView

1 在创建UITableView之前我通过懒加载的方式创建了一个数据源数组dataSourceArr,其中保存的是一些不同长度的字符串用来添加到cell中实现cell的不同的高度

2 添加UITableView,遵守数据源

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *mainTableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.mainTableView = mainTableView;
    [self.view addSubview:mainTableView];
    self.mainTableView.dataSource = self;
}

3 实现必须实现的两个数据源方法

// 返回每个区域中有几行cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
// 返回每个cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"MainTableViewCell";
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[MainTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    NSString *testStr = self.dataSourceArr[indexPath.row];
    // 以下的两个赋值方法千万不能把顺序写反了,因为我们是根据其展示的内容计算的高度
    cell.testStr = testStr;
    tableView.rowHeight = cell.rowHeight;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

其中testStr为该行中UILabel中将要展示的字符串,cell为下边自定义的cell。在这里我们将自定义cell中根据不同内容计算出来的每个cell的高度保存在cell的rowHeight属性中,并且通过tableView.rowHeight为每个cell的高度赋值,这样就可以在返回cell的数据源方法中为每个cell赋值不同的所需高度

自定义UITableViewCell并且根据内容计算高度

1 创建MainTableViewCell类,继承自UITableViewCell,自定义出cell的样式,在此案例中为了展示出不同的高度我只在其中添加了一个UILabel。开发中可以根据自己的需求创建不同的控件,计算高度的原理是相同的。

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configureCell];
    }
    return self;
}
// 创建自定义cell的内容控件
- (void)configureCell{
    UILabel *testLabel = [[UILabel alloc]init];
    self.testLabel = testLabel;
    [self addSubview:testLabel];
    testLabel.backgroundColor = [UIColor yellowColor];
    testLabel.font = [UIFont systemFontOfSize:16];
    testLabel.numberOfLines = 0;
}

2 在此类的.h文件中声明两个属性,一个为了传递UILabel中展示的字符串,一个为了保存计算出的每个cell的高度

@interface MainTableViewCell : UITableViewCell
// 保存每个cell的UILabel中展示的字符串
@property(copy,nonatomic)NSString *testStr;
// 保存计算出来的每个cell的高度
@property(assign,nonatomic)CGFloat rowHeight;
@end

3 通过重写赋值字符串的setter方法为每个UILable赋值,并计算出UILabel所需要的高度

- (void)setTestStr:(NSString *)testStr{
    _testStr = testStr;
    self.testLabel.text = testStr;
    // 调用计算控件布局和cell高度的方法
    [self setupSubViewConstrainWithStr:testStr];
}
- (void)setupSubViewConstrainWithStr:(NSString *)testStr{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    // 根据Label的内容计算所需要的高度,maxSize为控件的最大的宽高
    CGSize maxSize = CGSizeMake(width-20, CGFLOAT_MAX);
    CGRect rect = [testStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
    self.testLabel.frame = CGRectMake(10, 10, width-20, rect.size.height);
    // 如果此处有多个控件可以获取最后一个控件的最大Y值,也可以将里边的全部控件的高度加起来,之后保存到自己的rowHeight属性中
    // 此方法的核心就是在这里通过cell里边的控件计算出这个cell应有的高度,保存起来,然后在数据源方法中利用tableView.rowHeight方法为其赋值
    self.rowHeight = CGRectGetMaxY(self.testLabel.frame)+10;
}

最后实现不同内容cell展现了不同的高度,效果如下:
这里写图片描述

总结

动态计算cell高度的方法还有其他的,比如frameModel的方法,利用UITableView的代理返回cell高度的方法,但是我认为这种方法在开发中用起来比较简单方便,所以在这里做一下简单的介绍,其中的核心就是UITableView的rowHeight属性。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值