ios - 怎样根据内容动态改变TableViewCell的高度(图文并排)?
怪我咯
怪我咯 2017-04-17 14:36:08
[iOS讨论组]

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

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(8)
阿神

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

大家讲道理

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

伊谢尔伦

动态的计算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。

ringa_lee

..........

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-...

PHP中文网

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

迷茫

可以参考我的文章 : http://blog.csdn.net/samuelan...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号