UITableViewCell 动态高度计算 - 手动计算控件高度

1.创建Xib,画出Cell布局

2.创建Xib对应的Cell类

#import <UIKit/UIKit.h>
@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;
}

3.控制器

#import "YCViewController1.h"
#import "YCTableViewCell.h"

@interface YCViewController1 ()<UITableViewDelegate,UITableViewDataSource>

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

@implementation YCViewController1
/**
 *  通过手动计算Cell中每个控件的高度 得出Cell总高度
 */
- (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];

    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;
}

/**
 *  计算Cell高度,并缓存
 *
 *  @return 返回缓高度数组
 */
- (NSArray *)rowHeightArr{
    NSMutableArray *tmpArr = [@[] mutableCopy];
    for (NSString *title in self.titleArr) {
        CGFloat marginH = 8;
        CGFloat bottomViewH = 30;
        CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX);
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 3.0;
        // 计算label文字的实际高度
        CGFloat labelH = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17] , NSParagraphStyleAttributeName : style} context:nil].size.height;
        CGFloat height = labelH + marginH + bottomViewH + marginH;
        [tmpArr addObject:@(height)];
    }
    return tmpArr.copy;
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,216评论 30 472
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,484评论 2 4
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 该怎么说起今天的感受呢?似乎很难起头。 那就从早饭后和妈妈弟弟一起去看大姨妈开始吧。 大姨家住在周村坡,大姨大妈妈...
    七月紫苏阅读 229评论 0 0
  • 1、平平淡淡是真没错,只是在强劲撕裂和极度分化的社会发展进程中,你的低风险承受和应对的能力、你的低掌控自己命运的能...
    黑麦的光影部落阅读 252评论 0 0