UITableView通过AutoLayout自动计算行高

为了演示简单,cell中我们只包含了一个view和一个label,针对iOS8之前的iOS系统,我们需要进行如下设置:

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

    self.tableView.dataSource = self;

    self.tableView.delegate = self;

    self.tableView.estimatedRowHeight = 80;

    [self.tableView registerClass:[HomePageCell class] forCellReuseIdentifier:NSStringFromClass([HomePageCell class])];

    [self.view addSubview:self.tableView];

    

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view);

    }];


    #pragma mark - UITableViewDataSource


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

      return 50;

    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

      HomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomePageCell class])

                                                         forIndexPath:indexPath];

      [self cell:cell setUpWithIndexPath:indexPath];

      return cell;

    }


    #pragma mark - UITableViewDelegate


    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

   {

     static HomePageCell *cell;

     if (cell == nil) {

        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomePageCell class])];

     };

    

     [self cell:cell setUpWithIndexPath:indexPath];

    

     return [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;

   }


   - (void)cell:(HomePageCell *)cell setUpWithIndexPath:(NSIndexPath *)indexPath

   {

     NSUInteger row = indexPath.row;

    

     if (row % 2 == 0) {

        cell.topView.backgroundColor = [UIColor redColor];

        cell.titleLbl.text = @"孙杨世锦赛400米自由泳三连冠叶诗文无缘200米混合泳决赛";

     } else {

        cell.topView.backgroundColor = [UIColor greenColor];

        cell.titleLbl.text = @"中新社布达佩斯723日电 (记者 王婧)2017年国际泳联世锦赛游泳项目23日开赛。奥运冠军孙杨在男 子400米自由泳项目中夺得本届世锦赛游泳项目首金,中国“00小丫李冰洁在女子400米自由泳决赛摘铜,伦敦奥运冠军叶诗文无缘女子200米个人混合泳决赛。";

     }

   }

  下面是HomePageCell中的关键代码:

  

  - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

  {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    

    if (self) {

        _topView = [UIView new];

        _topView.backgroundColor = [UIColor redColor];

        [self addSubview:_topView];

        

        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.and.right.equalTo(self);

            make.top.equalTo(self).with.offset(10);

            make.height.equalTo(@(30));

        }];

        

        _titleLbl = [UILabel new];

        _titleLbl.preferredMaxLayoutWidth = 100;

        _titleLbl.numberOfLines = 0;

        _titleLbl.lineBreakMode = NSLineBreakByCharWrapping;

        [self addSubview:_titleLbl];

        

        [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(_topView.mas_bottom);

            make.left.equalTo(self);

            make.width.equalTo(@(100));

            make.bottom.equalTo(self);

        }];

        [_titleLbl setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

     }

    

     return self;

  }


  对于iOS8之后的版本,我们在tableView定义的时候要多加一句:

  

  self.tableView.rowHeight = UITableViewAutomaticDimension;

  

  在UITableViewDelegate代理方法,heightForRowAtIndexPath中直接返回UITableViewAutomaticDimension,代码如下:

  #pragma mark - UITableViewDelegate


  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

  {

    return UITableViewAutomaticDimension;

  }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值