根据文本量更改UITableViewCell高度 [英] Change the UITableViewCell Height According to Amount of Text

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

问题描述

我需要能够在UITableView中调整单个单元格的高度,使其符合其详细标签中的文本数量。

I need to be able to adjust the height of a single cell in my UITableView so that it fits the amount of text in its detail label.

我玩过以下但是它对我不起作用:

I have played with the following but it hasn't work for me:

如何在没有自定义单元格的情况下在UITableViewCell中包装文本

希望你能提供帮助,谢谢。

Hope you can help, thanks.

编辑:

尝试代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20;
}

这没有用,它显示了单元格上的整个字符串,但是单元格高度完全没有受到影响。

This hasn't worked, it shows the entire string on the cell, however the cell height isn't affected at all.

推荐答案

根据您提供的代码,我认为您只会增加单元格高度而不是cell.textLabel的高度。

Based on the code you have provided, I think you are increasing only the cell height and not the cell.textLabel's height.

理想情况下,您应该设置cell.textLabel的框架大小和单元格,以便查看单元格中的全文。

Ideally, you should set the frame size of cell.textLabel and the cell for you to see the full text in the cell.

一个简洁的方法来查看视图在大小方面的错误,是为了使它与背景颜色不同(尝试将cell.textLabel背景设置为黄色)和看看是否实际设置了高度。

A neat way to see whats wrong with a view in terms of size, is to color it different than the background (try setting cell.textLabel background to yellow) and see if the height is actually being set.

这是应该如何

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];

    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = cell.textLabel.font;
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    cell.textlabel.frame.size = labelSize; 
    cell.text = cellText;
}

希望这会有所帮助!

更新:这是一个相当古老的答案,此答案中的许多行可能已被弃用。

update: This is quite an old answer, and many lines in this answer may be deprecated.

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

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