根据UILabel的高度设置UITableViewCell的高度 [英] Set UITableViewCell's height depending on the height of a UILabel

查看:144
本文介绍了根据UILabel的高度设置UITableViewCell的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能告诉我如何根据UILabel的高度设置UITableViewCell的高度,我将不胜感激.

I would really appreciate it if you could tell me how I could set the height of a UITableViewCell depending on the height of a UILabel.

我当前用于设置UILabel高度的代码是:

My current code to set the UILabel's height is:

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
           cell.textLabel.numberOfLines = 0;
            cell.textLabel.text = [self getItemForKey:kSummary];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.textLabel.textColor = [UIColor colorWithRed:54.0f/255.0f green:54.0f/255.0f blue:54.0f/255.0f alpha:1.0f];
            CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
            CGSize labelSize = [[cell.textLabel text] sizeWithFont:[cell.textLabel font] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
            cell.textLabel.frame = CGRectMake( 0, 0, 280, labelSize.height);

推荐答案

您将需要实现UITableViewDelegate方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

You will need to implement the UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [self getItemForKey:kSummary];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    //You will need to define kDefaultCellFont
    CGSize labelSize = [text sizeWithFont:kDefaultCellFont 
                        constrainedToSize:constraintSize 
                            lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + ANY_OTHER_HEIGHT;
}

这篇关于根据UILabel的高度设置UITableViewCell的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆