UITableView最简单的高度自适应原理

在iOS开发过程中,UITableView是应用最为广泛的控件之一,而在实现过程中,如何动态的去计算每个单元格的高度往往会纠结着我们,我也遇到过同样的问题,经过多次的测试和阅读大量的资源,大概知道集中高度自适应的方法,它们归根到底还是对AutoLayout的应用。方法一:使用xib进行布局,添加约束;(不常用)二:代码手写,我在工程中使用的是Masonry添加约束,值得一提的是不管哪一种方式都要保证有一个控件能够撑起cell格。下面直接上代码。

这里的viewController.h来初始化表视图,

//
//  ViewController.m
//  TestTableView_JC
//
//  Created by 祝国庆 on 16/4/11.
//  Copyright © 2016年 祝国庆. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import "JC_TableViewCell.h"
#import "TestModel.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView_jc;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView_jc = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView_jc.delegate = self;
    self.tableView_jc.dataSource = self;
    _tableView_jc.backgroundColor = [UIColor grayColor];
    self.tableView_jc.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView_jc];
    [_tableView_jc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableView_jc.superview).with.offset(20);
        make.left.equalTo(_tableView_jc.superview.mas_left);
        make.right.equalTo(_tableView_jc.superview.mas_right);
        make.bottom.equalTo(_tableView_jc.superview.mas_bottom);
    }];
    
   <span style="color:#ff0000;"> </span><span style="background-color: rgb(255, 102, 102);">self.tableView_jc.estimatedRowHeight = 40;//估算高度</span>
   <span style="background-color: rgb(255, 102, 102);"> self.tableView_jc.rowHeight = UITableViewAutomaticDimension;<span style="color:#ff0000;"> </span>  //高度自适应最重要的两句话,不用再给高度了 </span>
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    JC_TableViewCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (testCell == nil)
    {
        testCell = [[JC_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        testCell.backgroundColor = [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 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.imgName = @"login_bg";

    }
    else if(indexPath.row == 1)
    {
        model.name = @"num.2";
        model.introduction = @"adfsgdhjklggfdsdafghjklhgfdsaDFGHJKLHGF";
        model.imgName = @"login_bg";
    }
    else if(indexPath.row == 2)
    {
        model.name = @"num.3";
        model.introduction = @"收到了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ了发哈看了好久放假哎活动房环球而强迫你覅偶后覅和企鹅王if好奇问废话IQ而恢复IQ";
        model.imgName = @"login_bg";

    }
    
    testCell.testModel = model;
    
    
    return testCell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

子类化cell,JC_TableViewCell.h

#import <UIKit/UIKit.h>
#import "TestModel.h"
@interface JC_TableViewCell : UITableViewCell

//@property(nonatomic, strong) TestModel *testModel;

@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *introductionLabel;
@property (strong, nonatomic) UIImageView *userPhoto;

@end


JC_TableViewCell.m文件

#import "JC_TableViewCell.h"
#import "Masonry.h"
@implementation JC_TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //...
        /*
         @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
         @property (weak, nonatomic) IBOutlet UILabel *introductionLabel;
         @property (weak, nonatomic) IBOutlet UIImageView *userPhoto;
         */
        _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];
   <span style="color:#ff0000;"><strong> /*
     *可以有部分控件高哦度固定,
     *但是必须有一个控件撑起来cell,
     *比如userPhoto控件从顶部出发,自我介绍控件高度没写,顶部与头像控件底部关联,底部等于cell的底部
     */</strong></span>
    //头像
    [_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);
<span style="background-color: rgb(255, 102, 102);">        make.bottom.equalTo(_introductionLabel.superview.mas_bottom);//底部等于cell的di'bu
</span>    }];
    self.nameLabel.text = _testModel.name;
    self.introductionLabel.text = _testModel.introduction;
    self.userPhoto.image = [UIImage imageNamed:_testModel.imgName];
    
}


运行结果如图所示



更多请关注本人微博,谢谢阅览



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值