这是在iPhone 0S 2.0上。 2.1的答案也很好,但我不知道有关表格的任何差异。

感觉应该可以在不创建自定义单元格的情况下获取文本,因为 UITableViewCell 默认包含 UILabel 。我知道如果我创建一个自定义单元格,我可以使它工作,但这不是我想要实现的 - 我想了解为什么我当前的方法不起作用。

我已经发现标签是按需创建的(因为单元格支持文本和图像访问,因此在必要时它不会创建数据视图),所以如果我这样做:

cell.text = @""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];

然后我得到一个有效的标签,但在那个(和lineBreakMode)上设置 numberOfLines 不起作用 - 我仍然得到单行文本。 UILabel 中有足够的高度来显示文本 - 我只是在 heightForRowAtIndexPath 中返回一个较大的高度值。

有帮助吗?

解决方案

这是一种更简单的方法,它适用于我:

cellForRowAtIndexPath:函数中。第一次创建单元格时:

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];
}

您会注意到我将标签的行数设置为0.这样可以使用尽可能多的行。

下一部分是指定 UITableViewCell 的大小,所以在 heightForRowAtIndexPath 函数中这样做:

- (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;
}

我在返回的单元格高度上添加了20,因为我喜欢在文本周围添加一点缓冲区。

其他提示

更新了Tim Rupe对iOS7的回答:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    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];

    NSAttributedString *attributedText =
        [[NSAttributedString alloc]
            initWithString:cellText
            attributes:@
            {
                NSFontAttributeName: cellFont
            }];
    CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    return rect.size.height + 20;
}

当我遇到同样的问题时,记录我的经验的简短评论/答案。尽管使用了代码示例,表格视图单元格高度正在调整,但单元格内的标签仍未正确调整 - 解决方案是我从自定义NIB文件加载我的单元格,这发生在之后 调整后的细胞高度。

我在NIB文件中设置了不包装文本的设置,标签只有1行; NIB文件设置覆盖了我在代码中调整的设置。

我接受的教训是确保始终牢记物体在每个时间点的状态 - 它们可能尚未创建! ......有人在线。

如果我们只在 UITableView 单元格中添加文本,我们只需要两个代理就可以使用(无需添加额外的 UILabels

1) cellForRowAtIndexPath

2) heightForRowAtIndexPath

这个解决方案对我有用: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{ 
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;

    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
    cell.textLabel.text = [mutArr objectAtIndex:indexPath.section];
    NSLog(@"%@",cell.textLabel.text);

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png" ]];

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    CGSize labelSize = CGSizeMake(200.0, 20.0);

    NSString *strTemp = [mutArr objectAtIndex:indexPath.section];

    if ([strTemp length] > 0)
        labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];

    return (labelSize.height + 10);
}

这里的字符串 mutArr 是一个可变数组,我从中获取数据。

编辑: - 这是我拍的数组。

mutArr= [[NSMutableArray alloc] init];

[mutArr addObject:@"HEMAN"];
[mutArr addObject:@"SUPERMAN"];
[mutArr addObject:@"Is SUPERMAN powerful than HEMAN"];
[mutArr addObject:@"Well, if HEMAN is weaker than SUPERMAN, both are friends and we will never get to know who is more powerful than whom because they will never have a fight among them"];
[mutArr addObject:@"Where are BATMAN and SPIDERMAN"];

现在,tableviews可以有自定义单元格。将表格视图设置如下

tableView.estimatedRowHeight = 85.0 //使用适当的估算值 tableView.rowHeight = UITableViewAutomaticDimension

Apple Reference

我使用以下解决方案。

数据在成员中单独提供:

-(NSString *)getHeaderData:(int)theSection {
    ...
    return rowText;
}

可以在 cellForRowAtIndexPath 中轻松完成处理。 定义单元格/定义字体并将这些值分配给结果“单元格”。 请注意, numberoflines 设置为“0”,这意味着需要。

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

    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
    cell.textLabel.text= [self getRowData:indexPath.section];
    cell.textLabel.font = cellFont;
    cell.textLabel.numberOfLines=0;
    return cell;
}

heightForRowAtIndexPath 中,我计算包装文本的高度。 绑定大小应与您的单元格宽度相关。对于iPad,这应该是1024。 适用于iPhone和iPod 320。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
    CGSize boundingSize = CGSizeMake(1024, CGFLOAT_MAX);
    CGSize requiredSize = [[self getRowData:indexPath.section] sizeWithFont:cellFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    return requiredSize.height;    
}

我发现这很简单直接前进:

[self.tableView setRowHeight:whatEvereight.0f];

例如:

[self.tableView setRowHeight:80.0f];

这可能是也可能不是这样做的最佳/标准方法,但它适用于我的情况。

在swift中尝试我的代码。此代码也适用于普通的UILabel。

extension UILabel {
    func lblFunction() {
        //You can pass here all UILabel properties like Font, colour etc....
        numberOfLines = 0
        lineBreakMode = .byWordWrapping//If you want word wraping
        lineBreakMode = .byCharWrapping//If you want character wraping
    }
}

现在就像这样打电话

cell.textLabel.lblFunction()//Replace your label name 

我认为这是一个更好,更短的解决方案。只需格式化单元格的 UILabel textLabel ),通过指定 sizeToFit 自动计算高度,一切都应该没问题。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...
    cell.textLabel.text = @"Whatever text you want to put here is ok";
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    [cell.textLabel sizeToFit];

    return cell;
}

我认为您不能操纵基础 UITableViewCell的私有 UILabel 来执行此操作。您可以自己将新的 UILabel 添加到单元格中,并使用 numberOfLines sizeToFit 来适当地调整大小。类似的东西:

UILabel* label = [[UILabel alloc] initWithFrame:cell.frame];
label.numberOfLines = <...an appriate number of lines...>
label.text = <...your text...>
[label sizeToFit];
[cell addSubview:label];
[label release];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top