【问题标题】:UILabel height inside UITableViewCellUITableViewCell 内的 UILabel 高度
【发布时间】:2013-02-09 02:27:57
【问题描述】:

我正在尝试创建一个表格视图,其中单元格的高度是动态的。

到目前为止,我设法根据我在里面添加的自定义 UILabel 设置单元格的高度。

使用常规的 cell.textLabel 可以正常工作,但是当我使用自己的标签时出现问题。我只看到一半的标签,但是当我上下滚动时,有时标签会延伸并显示所有文本......您可以看到标签应该在图像中结束的位置。

Image

这是cellForRowAtIndexPath里面的文字:

static NSString *CellIdentifier = @"Cell";

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

// Configure the cell.
Car *carForCell = [cars objectAtIndex:indexPath.row];

UILabel *nameLabel = [[UILabel alloc] init];
nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.numberOfLines = 0;
nameLabel.text = carForCell.directions;
[nameLabel sizeToFit];

[nameLabel setBackgroundColor:[UIColor greenColor]];


return cell;

【问题讨论】:

    标签: ios objective-c xcode uitableview


    【解决方案1】:

    除非您发布的代码中有拼写错误,否则您似乎根本没有将标签添加到单元格中。您似乎每次都在创建一个新标签,然后用单元格的视图替换您的 nameLabel 指针的内容(始终为 nil)。

    先尝试做这样的事情,然后看看它的样子:

    static NSString *CellIdentifier = @"Cell";
    
    UILabel *nameLabel;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        nameLabel = [[UILabel alloc] init];
        nameLabel.tag = 100;
    
        nameLabel.numberOfLines = 0;
        [nameLabel setBackgroundColor:[UIColor greenColor]];
    
        [cell.contentView addSubview:nameLabel];
    }
    else {
         nameLabel = (UILabel *)[cell viewWithTag:100];
    }
    
    // Configure the cell.
    Car *carForCell = [cars objectAtIndex:indexPath.row];
    
    nameLabel.text = carForCell.directions;
    [nameLabel sizeToFit];
    
    return cell;
    

    您还需要使用tableView:heightForRowAtIndexPath: 委托方法告诉 tableView 每个单元格的大小。这意味着再次获取相关的Car 对象并使用sizeWithFont:sizeWithFont:forWidth:lineBreakMode: 计算高度

    【讨论】:

    • 我已经在tableView:heightForRowAtIndexPath 方法中设置了单元格的高度,如图所示。我更改了cellForRowAtIndexPath 中的代码,结果如下:link。所有文本都被打印出来了,但宽度似乎搞砸了。
    • 我认为这是sizeToFit: 的问题 - 你能用sizeWithFont:sizeWithFont:forWidth:lineBreakMode: 计算标签大小吗?
    • 似乎工作正常。在将此标记为正确答案之前,我会再尝试一下。 :-)
    【解决方案2】:

    你如何设置单元格的高度?应该在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    【讨论】:

    • 我已经在heightForRowAtIndexPath 中设置了高度,而且效果很好。问题是标签不会拉伸,它只是在单元格的中间切开。
    【解决方案3】:

    您应该通过以下方法计算并返回 UITableViewCell 的高度:

    - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    在这里,您应该对单元格的高度进行初步计算。

    例如:

    CGSize textSize = [myString sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(320, 9999)];
    return textSize.height;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 2011-08-06
      • 2018-08-28
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多