开发过程中,会很少使用系统自带的cell,一般都会自定义cell,用来展示各式各样的界面布局,所以我们要自定义cell---------项目中用过很多种cell高度自适应的算法,都感觉挺麻烦的,这个方法相对来说简单易懂,希望对大家有帮助
1、创建存储数据类:
2、创建自定义cell类:.h文件
#import @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中的类方法,用来计算动态的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //取出model HHPollingItemsModel *model = self.items[indexPath.row]; return [HHPollingDetailCell heightWithModel:model]; }
好了,这就是cell高度自适应的全过程,亲测使用带有图片、文字或者图片加文字的cell高度自适应
下面就是自适应后的布局:
还有更简单更实用的,望多指教,谢谢!