【问题标题】:How to resize custom cell height dynamically如何动态调整自定义单元格高度
【发布时间】:2018-06-28 10:11:41
【问题描述】:

我想动态调整自定义单元格的高度。

在我的 tableView 中有三个自定义列:

  1. 产品名称
  2. 产品数量
  3. 产品价格

现在产品名称列不显示全名。它只显示任何适合的文本。但无法显示全名。

虽然我已将heightForRowAtIndexPath 设置如下:

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

    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

这是在我的CustomCell 类中设置的以下行

self.productNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.productNameLabel.numberOfLines = 0; 

示例:

NSString *prodName = @"This is my product name which may contain description with it";

所以在上面的例子中,我的UITableViewCell 只显示了这么多产品名称:This is my product,它转义了更多的字符串。

请参阅下面的附加属性部分和约束...

我想在Nutrient 列中设置产品名称

请帮帮我,尝试了所有可能的解决方案,但无法解决。

这样我的 uitableview 数据显示...但无法在营养素中显示全名。

【问题讨论】:

  • 展示你是如何设置约束的

标签: ios objective-c uitableview row-height


【解决方案1】:

在 Size Inspector 中为您的标签设置这些值(如图所示),不要为您的标签设置高度限制。

【讨论】:

    【解决方案2】:

    首先,estimatedRowHeight 应该设置为某个特定的数字。这是tableView 用于计算滚动指示器大小的估计值,但并未真正使用它来调整单元格大小。因此,理想情况下,您应该设置如下内容:

    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableViewAutomaticDimension
    

    然后确保您的单元格已定义可用于定义其大小(特别是高度)的自动布局约束。在您的情况下,请确保标签顶部锚点被约束到单元格的contentView.topAnchor,底部锚点被约束到单元格的contentView.bottomAnchor,并且标签的前导和尾随到contentView 的前导和尾随。因此,当标签有更多内容时,它会尝试扩展,单元格也应如此。

    考虑使用UIStackView 管理单元格的内容,因为这样即使单元格的展开和折叠也会更易于管理。如果这是您的目标,请参阅 my answer 了解另一个问题。

    【讨论】:

    • 请检查我编辑的问题,我已经尝试但尚未完成。
    • @Aadi 您展示这些限制的方式,我们无法阅读其中的一半.. 但有些东西绝对不行 - 营养标签的底部固定在一个叫做 NutritionTa..... 的东西上应该转到单元格的底部(标签的超级视图,就像标签的顶部一样)
    【解决方案3】:

    你做得很好,但还需要更多。首先你设置。 tableView.estimatedRowHeight = 44 在您获得等于 13 的底部约束之后,只需使其大于或等于 13。它将为您工作。

    【讨论】:

      【解决方案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
          table.rowHeight = UITableViewAutomaticDimension
          table.estimatedRowHeight = UITableViewAutomaticDimension
      }
      
      
      
      // UITableViewAutomaticDimension calculates height of label contents/text
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return UITableViewAutomaticDimension
      }
      

      对于 UITableviewCell 中的标签实例

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-27
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多