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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值