首页 > 怎样根据内容动态改变TableViewCell的高度(图文并排)?

怎样根据内容动态改变TableViewCell的高度(图文并排)?

类似朋友圈的内容,根据内容动态改变Cell的高度。


一篇文章解决你的问题:Auto Layout 使用心得(五)—— 根据文字、图片自动计算 UITableViewCell 高度


问题是当self.tableView.reloadData()更新数据后,Cell里的内容更新了,Cell的高度如何同时更新?


cell高度变化一般就是label 所有求出label高度即可
1. 对于纯代码手写我一般在相应的cell里写一个类方法

  1. 对了用storyboard的可以建好约束, 然后写了一个tableViewCell的分类求高度

@implementation UITableViewCell (HLMath)

@end
对于约束的建立 可以看这个教程http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-...


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *edit =[EndRequest objectAtIndex:indexPath.row];

    Test2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentitier];

    float h= cell.contentView.frame.size.height;
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width - 50,cell.contentView.frame.size.height+50, 50, 20)];
    [btn2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn2 setTitle:@"评论" forState:UIControlStateNormal];
    btn2.backgroundColor = [UIColor redColor];
    [btn2 addTarget:self action:@selector(btnClick2:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:btn2];


    cell.btn.tag = indexPath.row;
    cell.btn2.tag = indexPath.row;

    cell.userNameLabel.text=edit[@"nickname"];
    cell.timeLabel.text=edit[@"create_dateline"];
    cell.bodyLabel.text = edit[@"content"];

    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:edit[@"avatar"]]];

    NSArray *img = edit[@"imgs"];
    if(img.count>0){
        [cell setImageswithURLs:img];
    }

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];


    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [EndRequest objectAtIndex:indexPath.row];
    NSString *r = cellIdentitier;
    Test2TableViewCell *cell = [offscreenCells objectForKey:r];
    if (!cell) {
        cell = [[Test2TableViewCell alloc] init];
        [offscreenCells setObject:cell forKey:r];
    }

    cell.userNameLabel.text=dic[@"nickname"];
    cell.timeLabel.text=dic[@"create_dateline"];
    cell.bodyLabel.text = dic[@"content"];
    NSURL *url = [NSURL URLWithString:dic[@"avatar"]];

    [cell.headImageView sd_setImageWithURL:url];

    NSArray *imgAry = dic[@"imgs"];
    if(imgAry.count > 0){
        [cell setImageswithURLs:imgAry];
    }

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
    [cell setNeedsLayout];
    [cell layoutIfNeeded];
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+50;
     height += 1;

    return height;
}

为什么在 cellForRowAtIndexPath: 中添加 Button 时返回的 Cell.contentView.fram.size.height 一直都是 44.0。


动态的计算cell高度的
以下举例:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //打算比方
    NSArray *list = _subDictionary[key];//数据
    CellFrame *cellFrame = [[CellFrame alloc] init];
    cellFrame.list = list;//传入数据,在list的set方法中动态的计算cell的高度
    return cellFrame.cellHeight;//返回高度
}

这样写应该很清楚吧


..........

NSString下面这个方法算出来bounds,然后就不用我说了吧。。。

objc- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

可以参考一下我以前的一个回答。个人觉得比较详细了。
我用了两种不同的方式对cell进行根据动态内容改变相应的cell高度
tableViewcell 用autolayout约束的问题

【热门文章】
【热门文章】