美文网首页开发技术带我飞
UITableViewCell高度自适应探索--AutoLayo

UITableViewCell高度自适应探索--AutoLayo

作者: CoderAO | 来源:发表于2015-08-01 17:05 被阅读10764次

之前我们已经对Cell高度自适应进行了几次研究:
UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell
地址: http://www.jianshu.com/p/7839e3a273a6
UITableViewCell高度自适应探索--cell预估高度(一)
地址: http://www.jianshu.com/p/6ab92579fcf1
UITableViewCell高度自适应探索--cell预估高度(二)
地址: http://www.jianshu.com/p/f3609cd9392e
今天,再提供一种AutoLayout与Frame相结合的方式来设置cell高度的方法.

今天这个方法的要点是:

  • 使用Autolayout在进行布局.
  • 使用Frame进行高度计算
  • 使用模型的属性缓存每个Cell第一次计算的高度.

相对于之前说的那些方法,这个方法比UITableView+FDTemplateLayoutCell使用起来更简单和容易理解(自从写FD那篇文章发表后收到很多网友的关于使用的问题,大部分是由于没有使用正确);并且克服了预估高度方式的那些问题,也不用把约束改来改去, 使计算的过程更加可控.

这种方法虽然是使用fram的方式计算,但是如果没有autoLayout,计算的过程就会复杂几倍,有时候可能还需要一个专门的类去管理子控件的frame.在我看来是一个比较不错的方法.

进入正题.

先看要实现的效果:

目标效果

其中文字的长度不一,图片可能有或没有.为了排除其他干扰,数据来自plist文件.

  • 这是我们自定义cell的设置,这些元素是固定的,我们使用AutoLayout对它们几个进行布局.
cell布局
  • 创建一个Message模型,赋予其对应的属性.
    由于cell的高度本质上还是基于模型数据来算的,所以计算高度的任务交给模型,故模型同时开放一个cellHeight的只读属性,将来好拿给控制器使用.
@interface Message : NSObject

// 头像、名字、和描述文字我给固定了,所以没有弄属性处理
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, assign, readonly) CGFloat cellHeight;

@end
  • 模型计算Cell高度,通过重写cellHeight的getter方法实现
- (CGFloat)cellHeight {
    if (!_cellHeight) {
        CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * margin; // 屏幕宽度减去左右间距
        CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contentFont]}
                                                      context:nil].size.height;
        _cellHeight = contentLabelY + contentH + margin;
    }
    return _cellHeight;
}
注意:
上面高度的计算还没有将内容图片的高度计算在内.
并且实现了利用模型的cellHeight属性缓存第一次计算高度.
  • 由于内容图片不是每个cell都有,所以使用代码动态添加.
// 属性声明
@property (strong, nonatomic) UIImageView *contentImageView;
// getter实现
- (UIImageView *)contentImageView {
    if (!_contentImageView) {
        _contentImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_contentImageView];
    }
    return _contentImageView;
}
  • cell该接收模型了
@property (nonatomic, strong) Message *message;
- (void)setMessage:(Message *)message {
    _message = message;
    self.contentLabel.text = _message.content;
    if (message.imageName.length) {
        self.contentImageView.hidden = NO;
        self.contentImageView.image = [UIImage imageNamed:message.imageName];
    }
    else {
        self.contentImageView.hidden = YES;
    }
}

当然,这时候contentImageView当然是显示不出来的,因为我们还没有赠送它一个frame.那么它的frame从何而来呢?

一开始我们说过,计算要基于模型,所以接下来的思路是由模型算出imageView的frame,拿去给cell用.

回到模型cellHeight的getter方法,添加对imageName属性的处理:

// 图片将要展示的frame作为模型的其中一个属性
@property (nonatomic, assign) CGRect contentImageFrame;
- (CGFloat)cellHeight {
    if (!_cellHeight) {
        CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * margin; // 屏幕宽度减去左右间距
        CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contentFont]}
                                                      context:nil].size.height;
        _cellHeight = contentLabelY + contentH + margin;

        // 对imageName属性的处理
        if (self.imageName.length) {
            UIImage *image = [UIImage imageNamed:self.imageName];
            CGFloat imageH = image.size.height;
            CGFloat imageW = image.size.width;
            // 直接得出contentImageView应该显示的frame
            _contentImageFrame = CGRectMake(margin, _cellHeight, imageW, imageH);
            _cellHeight += imageH + margin;
        }
    }
    return _cellHeight;
}

当上面代码执行完毕,contentImageFrame就有值了.接着,回到cell的setMessage:方法给contentImageView赋值.

- (void)setMessage:(Message *)message {
    _message = message;
    self.contentLabel.text = _message.content;
    if (message.imageName.length) {
        self.contentImageView.hidden = NO;
        self.contentImageView.image = [UIImage imageNamed:message.imageName];
        // 给图片的frame赋值,这个值就是上面得到那个
        self.contentImageView.frame = _message.contentImageFrame;
    }
    else {
        self.contentImageView.hidden = YES;
    }
}
  • 这时候使用起来就非常轻松了
// 控制器tableView协议方法实现
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Message *message = self.dataList[indexPath.row];
    return message.cellHeight;
}

完整代码下载:https://github.com/CoderAO/AutoCellHeightMix

相关文章

网友评论

  • 奔跑的鸿:思路不错,视图控制器就可以简洁明了,之前都把控件的高度计算放在了视图控制器中,使得控制器好臃肿,而且,从你的demo中我终于明白了啥叫缓存机制(之前一直觉得高深莫测),原来就是每个cell的高度只需计算一次的,当滑动到上次滑过的cell时,模型将上次计算好的值直接返回(因为_cellHeight!=nil了就直接return)就算是缓存了!顿悟啊,阿里嘎多😊
  • lyonse:个人感觉这种方式虽然可行但是代价也是很大的.
    首先, model的混杂了与数据无关的东西这种设计本身并不是好方式.
    然后, 一般项目中调整界面布局的时候, 就需要修改model中的计算方法, 比如间隙, 富文本的情况, 这个时候调整起来一旦cell复杂, 计算方式可读性差, 维护起来就是比较难受了.
    还有, 个人感觉就算要计算cell的高度, 计算方式也不应写在model中,写在cell中更好,毕竟样式是与具体cell相关的嘛.
    个人感觉FDTemp那个思想应该才最优的方式
  • 奔跑的蔬菜:如果有多张图片 要怎么判断才好
    CoderAO:@奔跑的蔬菜 如果是在一个cell里面,累加起来就可以
  • 9bfe700d122a:如果你能回复我一下该多好 :blush:
  • 4ade35a4017d:很想知道初始化tableView 后,怎么拿到dataList的Count的值的
    CoderAO:@Usherock 不太明白您的问题,既然拿到datalist,count不是轻而易举么?
  • CoderAO:@19021105cc5e 服务器直接返回cell高度?
    yanhooIT:@CoderAO 应该不会的,服务算哪里知道那么多 :smile:
  • 穷得瑟M:开发中虽然大部分都是服务器直接返回宽高,不过学习下也不错
  • CoderAO:@斯卡 最好的办法是让服务器直接返回图片宽高
  • 斯卡:UIImage *image = [UIImage imageNamed:self.imageName];
    CGFloat imageH = image.size.height;
    CGFloat imageW = image.size.width;
    对于以上代码,在实际开发过程中,大部分图片是给一个URL地址要开发人员自己去获取的,所以这时候imageH,imageW不是那么快就能求出来的,假设这时候模型中的imageName换成URL类型,还有其他方法求出这个图片高度吗 :joy:
    csqingyang:@斯卡 如果使用了 SDWebImage,会自动帮你开启后台线程下载,在 completion 之后的 block 中获取下载的图片,开启后台线程进行裁剪。从产品角度出发,最好能让显示的 cell 上的图片能够有统一的尺寸。
    yanhooIT:@斯卡 这个可以让服务端给你返回的数据带图片的高度

本文标题:UITableViewCell高度自适应探索--AutoLayo

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