美文网首页
Masnory 动态计算Cell的高度.

Masnory 动态计算Cell的高度.

作者: follow_er | 来源:发表于2016-11-22 11:06 被阅读154次

Cell 文件里面 定义一个方法在.h文件中,下面是.m的实现

- (CGFloat)privateHeightForTableView{

    [self setNeedsLayout];
    [self layoutIfNeeded];

    CGFloat rowHeight = 0.0;
     for (UIView * bottomView in self.contentView.subviews) {
          if (rowHeight < CGRectGetMaxY(bottomView.frame)) {
                rowHeight = CGRectGetMaxY(bottomView.frame);
            }
       }
    
    rowHeight += 10;//这个10 是cell间距可以根据需求调整,没间距就不用添加了.
    return rowHeight;
}

在tableview的代理方法里面返回Cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MYCell *cell =  (MYCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return [cell privateHeightForTableView] ;
}

相关文章

网友评论

      本文标题:Masnory 动态计算Cell的高度.

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