UITableViewCell 动态高度计算 - Self Sizing Cells

1.Xib中设置约束,注意: "contentView的四边都必须和控件有约束设置",底部View要固定高度

UITableViewCell 动态高度计算 - Self Sizing Cells_第1张图片
Paste_Image.png
UITableViewCell 动态高度计算 - Self Sizing Cells_第2张图片
Paste_Image.png

2.estimatedRowHeight随便设置,最好和实际高度差不多,赋值下面两个属性之后就不要重写estimatedRowHeight和heightForCell两个TableView的代理方法了

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100.0f;
@interface YCTableViewCell2 : UITableViewCell
+ (instancetype)CellWithTitle:(NSString *)text tableView:(UITableView *)tableView;
@end

#import "YCTableViewCell2.h"

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

@end
@implementation YCTableViewCell2

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

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

@implementation YCViewController2

/**
 *  self sizing cells
 */
- (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.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 130.0f;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [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];
    YCTableViewCell2 *cell = [YCTableViewCell2 CellWithTitle:title tableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSLog(@"%f",cell.contentView.frame.size.height);
    return cell;
    
}

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

@end

你可能感兴趣的:(UITableViewCell 动态高度计算 - Self Sizing Cells)