【问题标题】:Label in dynamic cell height on table view not showing text properly表格视图中动态单元格高度中的标签未正确显示文本
【发布时间】:2013-12-23 23:31:10
【问题描述】:

我正在尝试让我的表格视图单元格根据单元格内的内容调整它们的高度。

似乎可以正常工作,因为单元格的高度不同,但我无法正确显示文本。

表格视图和原型单元格是使用情节提要创建的,并且我有一个自定义单元格类链接到我的原型单元格。在我的原型单元格中,我有三个标签;标题标签、日期标签和具有动态文本数据的标签。在情节提要的属性检查器中,动态文本数据标签行号设置为 0,换行符设置为“自动换行”。

当我运行应用程序时,单元格高度都不同,所以我假设索引路径处的行高度有效:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Text data
    NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];
    NSString *text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:nil];

    CGFloat width = 280;
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;

    return size.height + 2 * 20; // size.height plus 2 times the desired margin
}

这是我在索引路径处的行单元格:

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

    // If the cell doesn't exists create it
    if (cell == nil) {
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    // Text data
    NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];

    cell.titleLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"title"]];
    cell.dateLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"date"]];
    cell.newsTextLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];

    return cell;
}

打印出动态文本数据的cell.newsTextLabel.text显示在单元格中,但只显示第一行。

我尝试了一些为 iOS 6.1 显示动态单元格高度的示例,它们运行良好,但我无法让它在 iOS 7+ 上运行

所以我希望有人可以帮助解决这个问题并帮助我找出我做错了什么。

【问题讨论】:

  • 对可变文本长度的标签有什么限制?它需要绑定到单元格的底部,并绑定到其上方的标签(没有固定的高度约束),因此它将随着单元格展开。
  • @rdelmar 当然!哈哈,我完全忘了设置约束。感谢您向我指出这一点!您想将您的帖子作为答案,以便我可以接受它作为解决方案吗?再次感谢!
  • 拜托,你能解释一下如何设置约束吗?我也有同样的问题。

标签: ios objective-c swift uitableview row-height


【解决方案1】:

试试这个

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

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    float height = cell.frame.size.height;
    return height;
}

【讨论】:

  • 嗯,我不知道我的问题也许真的很清楚。我不认为单元格的高度是我的问题,或者我实际上还不知道,因为标签还没有打印出全文,但不同单元格的高度似乎不同。这就是我此刻的问题......
【解决方案2】:

要使标签随单元格一起扩展,您应该为其提供约束,将其与上方和下方的视图联系起来——在您的情况下,这看起来像是单元格底部的约束,以及标签的约束(使用“标题...”)在它上面。

【讨论】:

    【解决方案3】:
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByClipping;
    

    【讨论】:

      【解决方案4】:

      要为行高和估计行高设置自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高布局有效。

      • 分配和实现 tableview 数据源和委托
      • UITableViewAutomaticDimension 分配给rowHeight 和estimatedRowHeight
      • 实现委托/数据源方法(即heightForRowAt 并向其返回值UITableViewAutomaticDimension

      -

      目标 C:

      // in ViewController.h
      #import <UIKit/UIKit.h>
      
      @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
      
        @property IBOutlet UITableView * table;
      
      @end
      
      // in ViewController.m
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          self.table.dataSource = self;
          self.table.delegate = self;
      
          self.table.rowHeight = UITableViewAutomaticDimension;
          self.table.estimatedRowHeight = UITableViewAutomaticDimension;
      }
      
      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          return UITableViewAutomaticDimension;
      }
      

      斯威夫特:

      @IBOutlet weak var table: UITableView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
      
          // Don't forget to set dataSource and delegate for table
          table.dataSource = self
          table.delegate = self
      
          // Set automatic dimensions for row height
          // Swift 4.2 onwards
          table.rowHeight = UITableView.automaticDimension
          table.estimatedRowHeight = UITableView.automaticDimension
      
      
          // Swift 4.1 and below
          table.rowHeight = UITableViewAutomaticDimension
          table.estimatedRowHeight = UITableViewAutomaticDimension
      
      }
      
      
      
      // UITableViewAutomaticDimension calculates height of label contents/text
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          // Swift 4.2 onwards
          return UITableView.automaticDimension
      
          // Swift 4.1 and below
          return UITableViewAutomaticDimension
      }
      

      UITableviewCell 中的标签实例

      • 设置行数=0(&换行模式=截尾)
      • 设置与其父视图/单元格容器相关的所有约束(顶部、底部、左右)。
      • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

      注意:如果您有多个标签(UIElements)具有动态长度,应根据其内容大小进行调整:为您的标签调整'Content Hugging and Compression Resistance Priority'想要以更高的优先级展开/压缩。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2012-04-06
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        • 2019-09-12
        相关资源
        最近更新 更多