美文网首页
UITableViewCell的高度计算

UITableViewCell的高度计算

作者: aDou小夏 | 来源:发表于2020-05-14 14:03 被阅读0次

    UITableViewCell的高度分为两种,固定和不固定
    一.固定cell的高度
    直接在代理的方法中设置cell的高度

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 100;
    }
    

    二.不固定cell的高度
    可以用两种方式计算cell的高度
    1.第一种方法
    在获取到数据之后,就去计算cell的高度,然后储存到model里面
    首先在model中创建一个高度的属性

    @property(nonatomic,assign)CGFloat cellHeight;
    

    在自定义单元格的.h文件写一个类方法

    +(CGFloat)heightWithModel:(exampleModel *)model;
    

    在自定义单元格的.m中去实现此方法

    /** 根据model数据计算cell高度 */
    +(CGFloat)heightWithModel:(exampleModel *)model{
        NSString *strDetail = model.detail;
        //boundingRectWithSize 根据此方法,可以根据控件字体的大小计算出一个CGSize
        UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:13];
        CGFloat height = [strDetail boundingRectWithSize:CGSizeMake(TC_SCREEN_WIDTH-AdaptiveW(30), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.height;
        model.detailHeight = height;
        //uilabel的高度+图片的高度+上下的间距
        model.cellHeight = height+AdaptiveW(80)+AdaptiveW(70);
        return model.cellHeight;
    }
    

    在创建/获取数据的时候 直接把计算出来的高度储存到model中

    exampleModel *model2 = [[exampleModel alloc]init];
    model2.name = @"李白<<将进酒>>";
    model2.detail = @"在这首诗里,李白演绎庄子的乐生哲学,表示对富贵、圣贤的藐视。";
    model2.imageName = @"黄河";
    model2.cellHeight = [firstTableViewCell heightWithModel:model2];
    [array1 addObject:model2];
    

    通过heightForRowAtIndexPath来设置行高

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        exampleModel *model = _arrayExample[indexPath.row];
        return model.cellHeight;
    }
    

    问题注意:
    (1)先执行+(CGFloat)heightWithModel:(exampleModel *)model;方法计算高度,储存到model中
    (2)更新数据源-(void)updateCellData:(exampleModel *)model;
    (3)最后执行layoutSubviews,cell的size改变的时候,才会执行layoutSubviews
    (4)在计算单元格高度的时候,Masonry和frame可以同时使用,这样更方便一些,把需要计算的高度计算出来,其他的就用masonry.

    [_labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(AdaptiveW(15));
        make.top.equalTo(self.contentView).offset(AdaptiveW(10));
    }];
    [_labelLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(AdaptiveW(15));
        make.top.equalTo(_imageViewLeft.mas_bottom).offset(AdaptiveW(15));
        make.size.mas_equalTo(CGSizeMake(TC_SCREEN_WIDTH-AdaptiveW(30), 0.5));
        make.bottom.mas_equalTo(self.contentView).offset(-AdaptiveW(10));
    }];
    

    用同样的方式根据model数据计算cell高度

    +(CGFloat)heightWithModel:(exampleModel *)model{
        NSString *strDetail = model.detail;
        //boundingRectWithSize 根据此方法,可以根据控件字体的大小计算出一个CGSize
        UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:13];
        CGFloat height = [strDetail boundingRectWithSize:CGSizeMake(TC_SCREEN_WIDTH-AdaptiveW(30), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.height;
        model.detailHeight = height;
        //uilabel的高度+图片的高度+上下的间距
        model.cellHeight = height+AdaptiveW(80)+AdaptiveW(70);
        return model.cellHeight;
    }
    

    2.第二种方法 (适用于在iOS8.0及以后)
    (1)在iOS8以后,苹果为我们提供了一个estimatedRowHeight和UITableViewAutomaticDimension属性.
    (2)关于约束,我们的约束必须满足以下三个条件:
    2.1 使用Autolayout进行UI布局约束(要求cell.contentView的四条边都与内部元素有约束关系)。
    2.2 指定TableView的estimatedRowHeight属性的默认范围值
    2.3 指定TableView的rowHeight属性为UITableViewAutomaticDimension。
    其中estimatedRowHeight值预估的越接近单元格高度越好

        _exampleTableView.estimatedRowHeight = AdaptiveW(200);
        _exampleTableView.rowHeight = UITableViewAutomaticDimension;
    

    在单元格中,使用Masonry第三方对控件进行约束, Masonry的本质是对autolayout的封装,单元格的高度根据控件自上而下的约束.

    相关文章

      网友评论

          本文标题:UITableViewCell的高度计算

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