【问题标题】:Minimum cell height with UITableViewAutomaticDimensionUITableViewAutomaticDimension 的最小单元格高度
【发布时间】:2016-02-03 05:25:18
【问题描述】:

是否可以为单元格设置最小高度?我使用动态:

tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension

但是当我的news title label text 在一行中时,我需要为单元格设置最小高度。

【问题讨论】:

    标签: swift uitableview uitableviewautomaticdimension


    【解决方案1】:

    您是否尝试在自定义UITableViewCellheight >= 60.0 视图中创建约束?

    【讨论】:

    • 你到底是怎么解决的?你能提供一些说明吗?
    • 在 UITableViewCell 顶部拖放一个 View 并将前导、尾随、顶部和底部约束设置为 0。将高度约束设置为 >= ExpectedMinimumSize。
    • 你可以在没有额外视图的情况下做到这一点。如果你有一个大的图像和一个文本,只需将图像底部约束设置为 >= x
    【解决方案2】:

    知道了。使其工作如下。

    在 UITableViewCell 顶部拖放一个 View 并将约束 Leading、Trailing、Top 和 Bottom 设置为 0。 将高度约束设置为 >= ExpectedMinimumSize。

    In heightForRowAtIndexPath 委托方法:

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

    在 ViewDidLoad 中:

    self.tableView.estimatedRowHeight = 60; // required value.
    

    【讨论】:

    • 在 swift 3 中也为我工作:func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension; } 在 ViewDidLoad:self.tableView.estimatedRowHeight = 60
    • 我无法让它工作。我的单元格有一个标签和一个开关,两者都相对于超级视图垂直居中,没有垂直边距。在运行时,单元格的高度报告为44.6667,即使我指定了60 的估计高度(就像在这个答案中一样)。如果我在标签或开关中添加顶部/底部边距,它会起作用,但我必须计算这些以给出大约 60 并且感觉不优雅......
    【解决方案3】:

    @Hytek 回答了一个技巧。为此,您必须给出最小高度的约束。

    例如:如果您的表格单元格中有一个UILabel,并且您希望UILabel 根据动态内容增加高度。你有如下代码。

    tableView.estimatedRowHeight = 83.0
    tableView.rowHeight = UITableViewAutomaticDimension
    

    当内容较大时,它会增加您的标签高度,但当您的内容较小时,它也会降低。因此,如果您希望该标签应具有最小高度,那么您必须以height >= 30.0 对您的标签的方式为您的UILabel 提供高度限制。

    这样您的UILabel 的高度不会低于30.0

    【讨论】:

      【解决方案4】:

      在自定义单元格的自动布局代码处(界面生成器或以编程方式),添加适当的约束。

      例如(以编程方式在自定义单元格中)

          UILabel * label = [UILabel new];
          [self.contentView addSubview:label];
          NSDictionary * views = NSDictionaryOfVariableBindings(label);
      //Inset 5 px
          [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[label]-5-|" options:0 metrics:nil views:views]];
          [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[label]-5-|" options:0 metrics:nil views:views]];
      // height >= 44
          [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44.0]];
      

      【讨论】:

        【解决方案5】:
        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return (UITableView.automaticDimension > minimumHeight) ? UITableView.automaticDimension : minimumHeight
        }
        

        【讨论】:

        • 请考虑描述您为解决 OP 的问题所做的工作
        • 这种方法对我有点用,但在我的情况下自动调整大小变得禁用,这违背了目的。将行高保留为 UITableView.automaticDimension 并根据需要对视图设置高度约束。
        【解决方案6】:

        将 contentViews heightAnchor 设置为所需的最低高度。

        Swift 4.2 版本以编程方式

        contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: <Required least Height>).isActive = true

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-06
          • 2012-04-09
          • 2014-03-02
          • 2013-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多