美文网首页iOS开发iOS开发
iOS 开发--相对来说比较简单的cell高度自适应

iOS 开发--相对来说比较简单的cell高度自适应

作者: BUGLittlePC_hh | 来源:发表于2017-12-11 18:27 被阅读73次

开发过程中,会很少使用系统自带的cell,一般都会自定义cell,用来展示各式各样的界面布局,所以我们要自定义cell---------项目中用过很多种cell高度自适应的算法,都感觉挺麻烦的,这个方法相对来说简单易懂,希望对大家有帮助
1、创建存储数据类:


image.png

2、创建自定义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中的类方法,用来计算动态的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

还有更简单更实用的,望多指教,谢谢!

相关文章

网友评论

  • 北京阳宸电子技术公司技术支持:感觉每次都要计算高度,滑动的时候也要计算高度
  • 怪羊叔:老铁中间的复杂cell有demo看看嘛
    BUGLittlePC_hh:@宁静致远_77ed cell数据动态变化的,只要滑动cell就会计算高度,不管是写在控制器还是自定义cell中,影响性能是肯定的,因为一直在动态渲染,iOS没有安卓的那种高度自适应,必须计算,我的想法是这样的,不对的地方还望指正,谢谢
    北京阳宸电子技术公司技术支持:这样写性能会不会不好
    BUGLittlePC_hh:我这个是从整个项目中罗列出来的,没有单独的demo,cell不是很复杂,就是平常的注册cell,cell重用等等,就这部分代码:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld%ld", indexPath.section, indexPath.row];
    HHInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
    cell = [[HHInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    HHDisposeModel *model = self.listDataArray[indexPath.row];
    cell.model = model;
    return cell;
    }
    HHInfoTableViewCell这个是我自定义的cell类

本文标题:iOS 开发--相对来说比较简单的cell高度自适应

本文链接:https://www.haomeiwen.com/subject/fbqwixtx.html