Masonry自动计算cell行高:HYBMasonryAutoCellHeight

Masonry自动计算cell行高:HYBMasonryAutoCellHeight

2016-01-07 14:39 出处:清屏网 人气:79 评论(0

前言

还在手动计算 UITableViewCell 的行高吗?还在每次都因为需求变化一点就要大量调整 cell的高度而烦恼吗?现在教大家如何通过 Masonry 的自动布局来实现自动计算 cell 的行高!!!

在 github 没有找到基于 Masonry 自动计算行高的库,倒是找到了使用 xib/storyboard 在添加约束来自动计算行高的库,如: UITableView-FDTemplateLayoutCell

本人非常推崇 Masonry 来实现代码的自动布局,因此项目中都是使用 Masonry 布局的,为了自动计算行高,决定写一个扩展,以达到自动计算的效果,如此一来,开发者不用再关心那些非固定行高的 cell 的动态计算问题了。

设置关键依赖

要想自动计算出 cell 的行高,我们还需要指定以哪个视图作为 cell 的最后一个视图,比如我们最后要添加一条线,我们可以以这条线作为 hyb_lastViewInCell ,如果这条线还需要距离底部一定距离,那么可以设置 hyb_bottomOffsetToCell :

 
/**
* 必传设置的属性,也就是在cell中的contentView内最后一个视图,用于计算行高
* 例如,创建了一个按钮button作为在cell中放到最后一个位置,则设置为:self.hyb_lastVieInCell = button;
* 即可。
* 默认为nil,如果在计算时,值为nil,会crash
*/
@property (nonatomic, strong) UIView *hyb_lastViewInCell;
 
/**
* 可选设置的属性,默认为0,表示指定的hyb_lastViewInCell到cell的bottom的距离
* 默认为0.0
*/
@property (nonatomic, assign) CGFloat hyb_bottomOffsetToCell;
 

计算行高API

要计算行高,只需要在 UITableView 的计算行高的代理方法中调用此API即可:

 
/**
* 通过此方法来计算行高,需要在config中调用配置数据的API
*
* @param indexPath 必传,对应的indexPath
* @param confi     必须要实现,且需要调用配置数据的API
*
* @return 计算的行高
*/
+ (CGFloat)hyb_heightForIndexPath:(NSIndexPath *)indexPathconfig:(HYBCellBlock)config;
 

在调用时, config 传回来了 cell 对象,需要在调用处调用方法来配置好数据,才能正确地计算出 cell 的行高。通常是这样调用的:

 
- (CGFloat)tableView:(nonnullUITableView *)tableViewheightForRowAtIndexPath:(nonnullNSIndexPath *)indexPath {
  HYBNewsModel *model = nil;
  if (indexPath.row < self.dataSource.count) {
    model = [self.dataSourceobjectAtIndex:indexPath.row];
  }
 
  return [HYBNewsCellhyb_heightForIndexPath:indexPathconfig:^(UITableViewCell *sourceCell) {
    HYBNewsCell *cell = (HYBNewsCell *)sourceCell;
    
    // 配置数据
    [cellconfigCellWithModel:model];
  }];
}
 

实现例子

效果图如下:

我们看下实现 -initWithStyle: reuseIdentifier: 方法,因为我们要自动计算 cell 行高会自动调用此方法,因此一定要实现此方法来布局:

 
- (nonnullinstancetype)initWithStyle:(UITableViewCellStyle)style
                      reuseIdentifier:(nullableNSString *)reuseIdentifier {
  if (self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
    self.mainLabel = [[UILabel alloc]init];
    [self.contentViewaddSubview:self.mainLabel];
    self.mainLabel.numberOfLines = 0;
    [self.mainLabelsizeToFit];
    [self.mainLabelmas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(15);
      make.top.mas_equalTo(20);
      make.right.mas_equalTo(-15);
      make.height.mas_lessThanOrEqualTo(80);
    }];
    // 如果需要支持6.0,需要加上这句
//    self.mainLabel.preferredMaxLayoutWidth = ...
    
    self.descLabel = [[UILabel alloc]init];
    [self.contentViewaddSubview:self.descLabel];
    self.descLabel.numberOfLines = 0;
    [self.descLabelsizeToFit];
    [self.descLabelmas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(15);
      make.right.mas_equalTo(-15);
      make.top.mas_equalTo(self.mainLabel.mas_bottom).offset(15);
    }];
    // 如果需要支持6.0,需要加上这句
    //    self.mainLabel.preferredMaxLayoutWidth = ...
    
    self.button = [UIButtonbuttonWithType:UIButtonTypeSystem];
    [self.contentViewaddSubview:self.button];
    [self.buttonsizeToFit];
    [self.buttonsetTitle:@"我是cell的最后一个"forState:UIControlStateNormal];
    [self.buttonsetBackgroundColor:[UIColor greenColor]];
    [self.buttonmas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(15);
      make.right.mas_equalTo(-15);
      make.height.mas_equalTo(45);
      make.top.mas_equalTo(self.descLabel.mas_bottom).offset(40);
    }];
    
    // 必须加上这句
    self.hyb_lastViewInCell = self.button;
    self.hyb_bottomOffsetToCell = 20;
  }
  
  return self;
}
 

注意到这两行代码了吗:

 
self.hyb_lastViewInCell = self.button;
self.hyb_bottomOffsetToCell = 20;
 

先是设置哪个视图作为 cell 的最后一个视图,然后设置了最后一个参考视图与 cell 的底部的距离。其中, self.hyb_lastViewInCell 属性是必须要设置的,否则会抛出异常。

使用

这个组件是开源的,而且是支持 cocoapods 的,因此大家若是使用了 cocoapods 来管理项目第三方库,可以这样使用:

 
pod 'HYBMasonryAutoCellHeight', '~> 0.0.1'
 

如果项目未使用 cocoapods ,直接下载源代码,然后将 HYBMasonryAutoCellHeight 文件夹拖入工程即可使用!

源代码

大家可以到 github 下载源代码来看看,内部实现很简单,当然要实现自动计算行高也是有系统方法的,不一定需要像笔者这样来实现。

下载源代码: https://github.com/CoderJackyHuang/HYBMasonryAutoCellHeight

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值