開發(fā)過程中,會很少使用系統(tǒng)自帶的cell,一般都會自定義cell,用來展示各式各樣的界面布局,所以我們要自定義cell---------項目中用過很多種cell高度自適應的算法,都感覺挺麻煩的,這個方法相對來說簡單易懂,希望對大家有幫助
1、創(chuàng)建存儲數(shù)據(jù)類:
image.png
2、創(chuàng)建自定義cell類:.h文件
#import <UIKit/UIKit.h>
@class HHPollingItemsModel;
@interface HHPollingDetailCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UILabel *valueLabel;
@property (nonatomic, strong) HHPollingItemsModel *model;
//類方法,返回的值用來計算cell的高度
+ (CGFloat)heightWithModel:(HHPollingItemsModel *)model;
@end
.m文件
#import "HHPollingDetailCell.h"
#import "HHPollingItemsModel.h"
@implementation HHPollingDetailCell
//初始化自定義控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.font = [UIFont systemFontOfSize:14.0];
self.titleLabel.numberOfLines = 0;
self.titleLabel.textColor = kColor(164, 164, 164);
[self addSubview:self.titleLabel];
self.valueLabel = [[UILabel alloc] init];
self.valueLabel.font = [UIFont systemFontOfSize:14.0];
self.valueLabel.textColor = kColor(164, 164, 164);
[self addSubview:self.valueLabel];
_lineLabel = [[UILabel alloc] init];
_lineLabel.backgroundColor = [UIColor colorWithHexString:@"#E4EEF0"];
[self addSubview:_lineLabel];
}
return self;
}
//由于cell的布局特殊化,所有約束條件都在layoutSubviews方法中寫
- (void)layoutSubviews
{
[super layoutSubviews];
[self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.right.equalTo(self).with.offset(-15);
make.height.equalTo(@(14));
}];
//由于cell的高度是用該控件來撐開,所以不用給它高度約束
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).with.offset(10);
make.left.equalTo(self).with.offset(15);
make.right.equalTo(self).with.offset(-70);
}];
[_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self);
make.left.equalTo(self);
make.right.equalTo(self);
make.height.equalTo(@(1));
}];
}
//model的set方法,用來給自定義控件賦值
- (void)setModel:(HHPollingItemsModel *)model
{
if ([model.itemType isEqualToString:@"0"]) {
self.titleLabel.text = [NSString stringWithFormat:@"%@", model.itemName];
self.valueLabel.text = model.itemValue;
} else if ([model.itemType isEqualToString:@"1"]) {
self.titleLabel.text = [NSString stringWithFormat:@"%@/%@", model.itemName, model.itemContent];
self.valueLabel.text = model.itemValue;
} else {
self.titleLabel.text = [NSString stringWithFormat:@"%@", model.itemName];
self.valueLabel.text = model.itemValue;
}
}
//注意:這是寫這篇文章的重中之重,核心代碼
+ (CGFloat)heightWithModel:(HHPollingItemsModel *)model
{
HHPollingDetailCell *cell = [[HHPollingDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
[cell setModel:model];
[cell layoutIfNeeded];
CGRect frame = cell.titleLabel.frame;
return frame.origin.y + frame.size.height + 10;
}
@end
3、最后一步:在含有tableView的控制器中調用cell中的類方法,用來計算動態(tài)的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//取出model
HHPollingItemsModel *model = self.items[indexPath.row];
return [HHPollingDetailCell heightWithModel:model];
}
好了,這就是cell高度自適應的全過程,親測使用帶有圖片、文字或者圖片加文字的cell高度自適應
下面就是自適應后的布局:
image.png
image.png
還有更簡單更實用的,望多指教,謝謝!