iOS开发继iOS8后UITableView的Cell高度自适应

在之前的开发中我们需要首先计算出cell中控件内容高度,再去重写UITableView的CGFloat方法去调整cell的高度,iOS8后即可以通过autolayout的特性让UITableView的cell做到自适应高度。这里自己做了一些代码总结希望对大家有所帮助。

1、首先我们创建一个表

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.estimatedRowHeight = 40;//设置大约高度
    tableView.rowHeight = UITableViewAutomaticDimension;//设置tableview的cell高度自动调整
    tableView.delegate = self;
    tableView.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CustomCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (testCell == nil)
    {
        testCell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        testCell.backgroundColor = [UIColor colorWithRed:arc4random() % 110 * 0.1 green:arc4random() % 110 * 0.1 blue:arc4random() % 110 * 0.1 alpha:1.0];
    }
    testCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    TestModel *model = [[TestModel alloc]init];
    
    if (indexPath.row == 0)
    {
        model.name = @"numn.1";
        model.introduction = @"12345678976543214567854324567865432456786543245";
        model.user = @"login_bg.jpg";
        
    }
    else if(indexPath.row == 1)
    {
        model.name = @"num.2";
        model.introduction = @"遇着wwfdsfdgdsgfdhfdhggfgdsgdsfsw183888003024937325";
        model.user = @"login_bg.jpg";
    }
    else if(indexPath.row == 2)
    {
        model.name = @"num.3";
        model.introduction = @"收到了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅玩着啦啦啦啦啦啦啦了京津冀哈哈哈看iiiiii";
        model.user = @"login_bg.jpg";
        
    }
    
    testCell.testModel = model;
    
    
    return testCell;
    
}

2、自定义cell,并通过Masonry约束cell上的控件,再通过model给cell中自定义控件赋值

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _userPhoto = [[UIImageView alloc]init];
        _userPhoto.contentMode = UIViewContentModeScaleAspectFit;
        _userPhoto.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_userPhoto];
        
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:14.0];
        _nameLabel.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_nameLabel];
        
        _introductionLabel = [[UILabel alloc]init];
        _introductionLabel.backgroundColor = [UIColor lightGrayColor];
        _introductionLabel.font = [UIFont systemFontOfSize:14.0];
        _introductionLabel.numberOfLines = 0;
        [self.contentView addSubview:_introductionLabel];
    }
    return self;
}
- (void)setTestModel:(TestModel *)testModel
{
    if (_testModel != testModel)
    {
        _testModel = testModel;
        [self layoutIfNeeded];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    /*
     *可以有部分控件高哦度固定,
     *但是必须有一个控件撑起来cell,
     *比如userPhoto控件从顶部出发,自我介绍控件高度没写,顶部与头像控件底部关联,底部等于cell的底部
    */
    //头像
    [_userPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(10);
        make.width.height.mas_equalTo(60);
    }];
    //用户名
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_userPhoto.mas_right).with.offset(5);
        make.width.mas_equalTo(100);
        make.centerY.equalTo(_userPhoto.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    //自我介绍
    [_introductionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.equalTo(_introductionLabel.superview.mas_right).with.offset(-10);
        make.top.equalTo(_userPhoto.mas_bottom).with.offset(10);
        make.bottom.equalTo(_introductionLabel.superview.mas_bottom);//底部等于cell的di'bu
    }];
    self.nameLabel.text = _testModel.name;
    self.introductionLabel.text = _testModel.introduction;
    self.userPhoto.image = [UIImage imageNamed:_testModel.user];
    
}

运行结果展示:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值