UITableViewCell 动态高度计算 - Xib

注意:在实际开发中的话 建议不要用数组来缓存行高,用字典,indexPath来作为Key这样的话会更好一点。

Paste_Image.png
Paste_Image.png

1.创建xib并约束好子控件

2.创建xib对应的cell文件

@interface YCTableViewCell : UITableViewCell
+ (instancetype)CellWithTitle:(NSString *)text tableView:(UITableView *)tableView;
- (CGFloat)cellHeight;
@end
#import "YCTableViewCell.h"

@interface YCTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@end

@implementation YCTableViewCell

+ (instancetype)CellWithTitle:(NSString *)text tableView:(UITableView *)tableView{
    YCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YCTableViewCell" owner:self options:nil] firstObject];
        cell.label.text = text;
    }
    return cell;
}

/**
 *  将bottomView的最大Y值作为高度返回
 *
 *  @return 高度
 */
- (CGFloat)cellHeight{
    // 强制布局前先获取真是宽度,便于精确计算
    CGRect frame = self.contentView.frame;
    frame.size.width = [UIScreen mainScreen].bounds.size.width;
    self.contentView.frame = frame;
    // 强制布局
    [self layoutIfNeeded];
    
    //这里有一个坑,如果是服务器返回的json中带有/r/n的话,这里的计算会很不准确,所以采用下面的方式去重新计算高度
    //return CGRectGetMaxY(self.bottomView.frame);

    //根据sizeThatFits,(kScreenW-50)计算控件实际宽度
    CGFloat height = [self.detail sizeThatFits:CGSizeMake(kScreenW - 50, MAXFLOAT)].height;
    return height + 7 + CGRectGetMinY(self.detail.frame);
}
@end

3.处理tableView

#import "ViewController.h"
#import "YCTableViewCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
/**
 *  文字数组(cell数量)
 */
@property (nonatomic, strong) NSArray *titleArr;
/**
 *  缓存cell高度
 */
@property (nonatomic, strong) NSMutableArray *rowHeightArr;
@end

@implementation ViewController
/**
 *  通过Xib动态计算高度
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = self.titleArr[indexPath.row];
    YCTableViewCell *cell = [YCTableViewCell CellWithTitle:title tableView:tableView];
    // 获取高度
    CGFloat tmpHeight = [cell cellHeight];
    // 缓存高度
    [self.rowHeightArr addObject:@(tmpHeight)];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.rowHeightArr[indexPath.row] integerValue];
}

/**
 *  调用这个方法之后,会先执行cellForRowAtIndexPath而不是heightForRowAtIndexPath
 */
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
    }
    return _tableView;
}

- (NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = @[@"一一一一一一一一一一一",@"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一",@"一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一"];
    }
    return _titleArr;
}

- (NSMutableArray *)rowHeightArr{
    if (!_rowHeightArr) {
        _rowHeightArr = [NSMutableArray array];
        NSLog(@"%zd",_rowHeightArr.count);
    }
    return _rowHeightArr;
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 13,003评论 3 38
  • UITableViewCell 父类是UIView UITableView的每一行都是一个UITableViewC...
    翻这个墙阅读 11,693评论 0 1
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 5,274评论 0 1
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 10,300评论 1 9
  • 您在那边还好吗? 已经一年多没看见您的容颜,没听见您的呼唤。您走后,我的脾气变得越来越暴躁,甚至有的时...
    阿喵爱吃鱼1991阅读 1,882评论 0 0

友情链接更多精彩内容