ios tableview 自动计算cell高度

时间:2022-11-13 10:57:38

效果图:

ios tableview 自动计算cell高度


#import "TFTableViewController.h"

@interface TFTableViewCell ()

@property(nonatomic,copy)NSString *messageText;

@end

@implementation TFTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (!self) {
return nil;
}

self.textLabel.adjustsFontSizeToFitWidth = YES;
self.textLabel.textColor = [UIColor darkGrayColor];
self.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
self.detailTextLabel.numberOfLines = 0;
self.selectionStyle = UITableViewCellSelectionStyleGray;

return self;
}

- (void)setPost:(NSString *)messageStr
{
self.messageText = messageStr;

self.textLabel.text = @"123";
self.detailTextLabel.text = messageStr;

[self setNeedsLayout];
}

//自动计算cell的高度
+ (CGFloat)heightForCellWithPost:(NSString *)text {
return (CGFloat)fmaxf(70.0f, (float)[self detailTextHeight:text] + 45.0f);
}

//自动计算cell的高度
+ (CGFloat)detailTextHeight:(NSString *)text {
CGRect rectToFit = [text boundingRectWithSize:CGSizeMake(240.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0f]} context:nil];
return rectToFit.size.height;
}

#pragma mark - UIView

- (void)layoutSubviews {
[super layoutSubviews];

self.imageView.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f);
self.textLabel.frame = CGRectMake(70.0f, 6.0f, 240.0f, 20.0f);

CGRect detailTextLabelFrame = CGRectOffset(self.textLabel.frame, 0.0f, 25.0f);
CGFloat calculatedHeight = [[self class] detailTextHeight:self.messageText];
detailTextLabelFrame.size.height = calculatedHeight;
self.detailTextLabel.frame = detailTextLabelFrame;
}

@end

@interface TFTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)NSArray *messageArray;
@property(nonatomic, strong)UITableView *tfTableView;
@property(nonatomic,strong)UIRefreshControl *refreshControl;

@end


@implementation TFTableViewController

#pragma mark - life cycle

- (void)viewDidLoad
{
[super viewDidLoad];

self.messageArray = @[@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调调",@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调",@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调调",@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调",@"1234567"];

self.tfTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kDEVICEWIDTH, kDEVICEHEIGHT - 64) style:UITableViewStylePlain];
self.tfTableView.backgroundColor = [UIColor whiteColor];
self.tfTableView.delegate = self;
self.tfTableView.dataSource = self;
[self.view addSubview:self.tfTableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(__unused UITableView *)tableView
numberOfRowsInSection:(__unused NSInteger)section
{
return (NSInteger)[self.messageArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

TFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.post = self.messageArray[(NSUInteger)indexPath.row];

return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(__unused UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//自动计算cell的高度
return [TFTableViewCell heightForCellWithPost:self.messageArray[(NSUInteger)indexPath.row]];
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}



@end