UITableViewCell 动态高度计算 - Self Sizing Cells

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

Paste_Image.png
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 ()<UITableViewDelegate,UITableViewDataSource>
@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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容