【问题标题】:Change static tableview cell height dynamically动态更改静态表格视图单元格高度
【发布时间】:2020-01-13 10:28:46
【问题描述】:

我有一个表格视图。我从情节提要创建它,它是一个静态表格视图。(我将它用于配置文件设置)。

但我必须动态更改此表格视图第一行的高度,我不想更改其他单元格的高度。 他们应该使用他们的故事板高度值。 strong> 我该怎么做?

我尝试了以下代码:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 {
        return 100
    }
    return (tableView.cellForRow(at: indexPath)?.bounds.height)!
}

但这是为所有单元格设置 100。

我的表格视图:

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

使用下面的覆盖函数..

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
    return 100
}
    return UITableView.automaticDimension
}

但是您必须确保静态 tableview 中的每个项目都有(上/下/左/右)约束。只有当它有所有这些限制时它才有效。

【讨论】:

  • 如果我对零行也使用自动尺寸并更改零行的高度约束会发生什么?
  • 如果您正确设置了视图/单元格的约束,则无需调用heightForRowAt 方法。或者只是放在这里return UITableView.automaticDimension
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2015-04-16
  • 1970-01-01
相关资源
最近更新 更多