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