如何在 Swift 3 中计算或自动拟合 UiTableView 的高度



My TableView content 3 controller.

1( 网页视图 1

2( 图像视图

3( 网页视图 2

我正在动态设置所有 3 个控制器的帧大小。因此,应根据内容大小更改行高。

                    self.secondWebViewLable.frame = CGRect(x: 0,
                                                       y: Int(self.firstImage.frame.origin.y + self.firstImage.frame.height),
                                                       width: Int(screenSize.width),
                                                       height: Int(self.secondWebViewLable.frame.height) )
self.firstImage.frame  = CGRect(x: 0,
                                                        y: Int(self.firstWebViewLable.frame.origin.y + self.firstWebViewLable.frame.height),
                                                        width: Int(screenSize.width),
                                                        height: Int(scaledHeight) )

您可以看到控制器高度正在根据数据而变化。我也在尝试设置单元格的高度。下面是我的代码。但我固定了高度。请帮助我。

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      return screenSize.height
    }

使用自动布局

viewDidLoad(( 中设置估计的行高,并从 heightForRowAt indexPath 方法返回 UITableViewAutomaticDimension

有关更详细的说明,请参阅本教程:自调整大小表视图单元格

无自动布局

如果没有自动布局,您必须通过添加单元格中所有 UI 组件的高度和边距来计算每个单元格的高度,并在 UITableView 数据源方法表中返回计算的高度视图:高度为行索引路径:

最新更新