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

查看:27
本文介绍了根据文本量更改 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 中

尝试的代码:

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天全站免登陆