0

如何根据其中的内容量缩放 UITableViewCells?在我的单元格中,我使用了 3 个代表论坛的标签。这些标签被命名为“别名”、“日期”和“评论”。第三个标签注释,可以是任意数量的行。因此,我的单元格需要根据“评论”标签中的文本数量(大小)而动态变化。以下是我的相关代码:

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Feedback *item = [self.items objectAtIndex:indexPath.row];

UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];

return cell;
}

在这种方法中,我应该根据commentLabel 高度设置tableViewCell 的高度。唯一的问题是我不知道该怎么做?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (????) {

    return ????;
    //return [indexPath row] + 50;

}

return ????;
}

我需要有人试图查看我的代码并举例说明如何使这项工作发挥作用。毕竟,它只有 1 个标签....我已经看过几个教程但无法使其工作...感谢您的时间和帮助。这是一个带有一些旧代码的旧屏幕。这或多或少是论坛现在的样子:http ://tinypic.com/view.php?pic=16h7me9&s=6

4

1 回答 1

0

您需要的是一种计算使用特定字体呈现的文本大小的方法。幸运的是,UIKit 通过NSString UIKit AdditionssizeWithFont:中的各种方法为您提供了这一点。

于 2012-10-29T13:24:06.907 回答