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