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

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

阅读 18.8k
8 个回答

动态的计算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;//返回高度
}

这样写应该很清楚吧

图片描述

-(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。

..........

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

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

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

  • (CGFloat)heightForText:(NSString *)text{
    NSInteger MAX_HEIGHT = 10000;
    static UILabel *cellLabelView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    cellLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, MAX_HEIGHT)];
    cellLabelView.font = [UIFont systemFontOfSize:12];
    cellLabelView.numberOfLines = 0;
    });
    cellLabelView.text = text;
    CGFloat padding = 100;
    return [cellLabelView sizeThatFits:CGSizeMake(280, MAX_HEIGHT)].height + padding;
    }
    保证这里的label和你cell里用的cell的lable一样就可以了
  1. 对了用storyboard的可以建好约束, 然后写了一个tableViewCell的分类求高度

@implementation UITableViewCell (HLMath)

  • (CGFloat)calculateHeight {

    self.bounds = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 0);

    [self setNeedsLayout];
    [self layoutSubviews];

    CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height;
    }

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

新手上路,请多包涵

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

新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题