【问题标题】:Adjust UITableViewCell height according to the resizing label根据调整大小标签调整 UITableViewCell 高度
【发布时间】:2016-05-12 15:17:00
【问题描述】:

我有一个自定义的UITableViewCell。在 UITableViewCell 的contentView 中,我添加了一个UIButton,在按钮下方有一个UILabel(按钮和UILabel 是彼此的兄弟)。

一开始 UILabel 上没有文本,因此它的高度限制设置为 >= 0。但是当我点击按钮时,我设置了 UILabel 的文本属性,它应该变得可见。换句话说,我想扩大 UILabel 的高度,也应该扩大 UITableViewCell 的高度。

我已将 UIButton Top,Leading 和 Trailing 边缘固定到 UITableViewCell contentView,同样我已将 UILabel 的 Bottom,Leading 和 Trailing 边缘固定到 UITableViewCell contentsView。

现在的问题是,当我点击UITableViewCell 时,它不会调整UILabel 的大小,也不会调整UITableViewCell。

这是我的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


  MYCustomCell *cell = (MYCustomCell *) [tableView cellForRowAtIndexPath:indexPath];
  cell.descriptionLbl.text = @"This is my text";
  [cell layoutSubviews];
}

同样

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

  MYCustomCell *cell = (MYCustomCell *) [tableView cellForRowAtIndexPath:indexPath];
  cell.descriptionLbl.text = @"";
  [cell layoutSubviews];
}

这是我的自定义单元格的 layoutSubViews

- (void) layoutSubviews {
  [super layoutSubviews];

  self.descriptionLbl.preferredMaxLayoutWidth = CGRectGetWidth(self.contentView.bounds);
  self.descriptionLbl.numberOfLines = 0;
  [self.descriptionLbl sizeToFit];
  if (self.descriptionLbl.text.length > 0){
    self.descriptionLbl.frame  = CGRectMake(self.descriptionLbl.frame.origin.x, self.descriptionLbl.frame.origin.y, self.descriptionLbl.frame.size.width, 50); // Here the size will be calculated more accurately later
  }
}

请注意,我没有在任何地方设置UITableViewCell 的高度。

【问题讨论】:

    标签: ios uitableview autolayout uilabel


    【解决方案1】:

    最终你需要在你的 -viewdidLoad 中执行此操作

    [_tableview setRowHeight:UITableViewAutomaticDimension];
    [_tableview setEstimatedRowHeight:44];
    

    UITableViewAutomaticDimension 表示您的单元格高度是动态的,setEstimatedRowHeight 让您计算滚动高度

    【讨论】:

    • 顶视图(按钮)顶边距必须与 contentView 的顶边距对齐,底视图(标签)的底边距必须与 contentView 的底边距对齐。确保这一点
    • 这有什么不同吗?我的意思是我已经有了我的 Button.top = contentView.top 和类似的 label.bottom = contentView.bottom
    【解决方案2】:

    在您的情况下,您必须手动管理自动布局约束,仅当按钮可见且单击文本后可见。 我已经为你准备了一个演示,你可以在这里找到。

    https://github.com/rushisangani/TableViewCellExpand

    请参考 TableViewCell 的故事板约束并明智地管理它。

    【讨论】:

    • 嗨,它似乎工作。谢谢,但我想了解您如何使用高度约束的优先级来显示和隐藏它。你能给我解释一下吗?
    • 是的,基本上你必须为你想要折叠的视图设置低优先级。以及该视图的子视图。
    • 你能告诉我一些文档中提到的地方吗,或者你想出了一些其他的技巧。我仍然无法知道如何使用优先级来折叠它。
    • 您需要了解自动布局中的抗压缩性和内容拥抱优先级。这里只是一个例子,你可以从中学到东西。 krakendev.io/blog/autolayout-magic-like-harry-potter-but-real
    • 是的,我理解他们。但是您在这里创建的出口。是内容拥抱优先还是抗压优先?
    【解决方案3】:

    在表格视图单元格中设置动态大小的视图时,正确设置所有约束非常重要。开发人员最常犯的错误是忽略单元格的底部垂直约束。

    要更详细地了解如何在自定义 UITableViewCells 中设置动态调整大小的 UILabel,请查看我在here 写的帖子。

    【讨论】:

      【解决方案4】:

      Swift 2.0:

      添加这个:

      override func viewDidLoad() {
          super.viewDidLoad()    
          self.tableView.estimatedRowHeight = 80
          self.tableView.rowHeight = UITableViewAutomaticDimension
          }
      

      或

      func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      
              self.tableView.estimatedRowHeight = 80
              self.tableView.rowHeight = UITableViewAutomaticDimension
      }
      

      如果是 Table View Controller,请使用:

      func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      self.tableView.setNeedsLayout()
      self.tableView.layoutIfNeeded()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多