| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
8年前发布

Masonry自动计算cell行高:HYBMasonryAutoCellHeight

前言

还在手动计算 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];    }];  }   

实现例子

效果图如下:

Masonry自动计算cell行高:HYBMasonryAutoCellHeight

我们看下实现 -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

喜欢就给个star

关注我

如果在使用过程中遇到问题,或者想要与我交流,可加入有问必答 QQ群: 324400294

关注微信公众号: iOSDevShares

关注新浪微博账号:标哥Jacky

支持并捐助

如果您觉得文章对您很有帮助,希望得到您的支持。您的捐肋将会给予我最大的鼓励,感谢您的支持!

支付宝捐助 微信捐助
Masonry自动计算cell行高:HYBMasonryAutoCellHeight Masonry自动计算cell行高:HYBMasonryAutoCellHeight
</div>

来自: http://www.henishuo.com/masonry-cell-height-auto-calculate/

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1451377029120.html
Masonry iOS开发 移动开发