简体   繁体   English

iOS自动布局:表格视图单元格的动态高度

[英]iOS Auto-Layout: Dynamic height for table view cell

I have a table view with a bunch of cells (custom cell, which only has its content view). 我有一个包含一堆单元格的表视图(自定义单元格,只有其内容视图)。

In tableView:cellForRowAtIndexPath: I'm adding a predefined UIView (which has several subviews) to the content view of the custom cell. tableView:cellForRowAtIndexPath:我将预定义的UIView(有几个子视图)添加到自定义单元格的内容视图中。 I set up all constraints for the UIView and its subviews before. 我之前为UIView及其子视图设置了所有约束。

Last but not least, I set the vertical and horizontal constraints for the content view of my custom cell (superview) and the UIView, which was added before (subview). 最后但并非最不重要的是,我为自定义单元格(superview)的内容视图和之前添加的UIView(子视图)设置了垂直和水平约束。

The constraint strings look like this: 约束字符串如下所示:

    H:|[view]|
    V:|[view]|

Unfortunately, I still get the default height for all table view cells. 不幸的是,我仍然获得所有表视图单元格的默认高度。 I'm wondering If there's a way to let auto layout do the calculation of the height automatically according to content size. 我想知道是否有办法让自动布局根据内容大小自动计算高度。

Check out my detailed answer to this question here: https://stackoverflow.com/a/18746930/796419 在这里查看我对这个问题的详细答案: https//stackoverflow.com/a/18746930/796419

It takes a bit of work to set up, but you can absolutely have Auto Layout constraints driving a completely dynamic table view without a single hardcoded height (and let the constraint solver do the heavy lifting and provide you with the row height). 设置需要一些工作,但是您可以完全使用自动布局约束来驱动完全动态的表视图而不需要单个硬编码高度(并且让约束求解器完成繁重的工作并为您提供行高)。

Auto Layout won't help with the cell height. 自动布局无助于单元格高度。 You'll need to set that in tableView:heightForRowAtIndexPath . 您需要在tableView:heightForRowAtIndexPath设置它。 I guess you're probably asking this because your cell heights are variable, not fixed. 我想你可能会问这个因为你的细胞高度是可变的,而不是固定的。 ie, they depend on the content. 即,它们取决于内容。

To resolve that, pre-calculate the cell heights and store them in an array. 要解决此问题,请预先计算单元格高度并将其存储在数组中。 Return the value for the appropriate indexPath in the tableView:heightForRowAtIndexPath method. 返回tableView:heightForRowAtIndexPath方法中相应indexPath的值。

Be sure to calculate content sizes on the main thread, using sizeThatFits of UILabel classes and such like. 一定要使用UILabel类的sizeThatFits等计算主线程的内容大小。

If your calculation is intensive, do the majority of it off main apart from the view related methods such as sizeThatFits . 如果您的计算是密集的,除了与视图相关的方法(如sizeThatFits之外,请将其中的大部分工作放在main

I solved the problem by using CGSize size = [view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 我通过使用CGSize size = [view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];解决了这个问题CGSize size = [view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; in tableView:heightForRowAtIndexPath: . tableView:heightForRowAtIndexPath: .

To set automatic dimensions for row height, ensure following steps to make, auto dimension effective for cell/row height layout. 要设置行高的自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高度布局有效。

  • Assign and implement dataSource and delegate 分配和实现dataSource和委托
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight UITableViewAutomaticDimension分配给rowHeight和estimatedRowHeight
  • Implement delegate/dataSource methods (ie heightForRowAt and return a value UITableViewAutomaticDimension to it) 实现delegate / dataSource方法(即heightForRowAt并向其返回值UITableViewAutomaticDimension

Swift: 迅速:

@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
}

For label instance in UITableviewCell 对于UITableviewCell中的标签实例

  • Set number of lines = 0 (& line break mode = truncate tail) 设置行数= 0(&换行模式=截断尾部)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container. 设置相对于其superview / cell容器的所有约束(顶部,底部,右侧)。
  • Optional : Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data. 可选 :如果您希望标签覆盖最小垂直区域,即使没有数据,也可以设置标签的最小高度。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM