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]; }]; }
實現例子
效果圖如下:
我們看下實現 -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
支持并捐助
如果您覺得文章對您很有幫助,希望得到您的支持。您的捐肋將會給予我最大的鼓勵,感謝您的支持!
支付寶捐助 | 微信捐助 |
---|---|
![]() |
![]() |
來自: http://www.henishuo.com/masonry-cell-height-auto-calculate/