【问题标题】:Self sizing specific UITableView Cell自行调整特定 UITableView 单元的大小
【发布时间】:2017-09-17 18:24:38
【问题描述】:

我知道如何在 UITableView 中设置 UITableViewCells 的大小。问题是如何仅对特定的单元格自行调整大小。我的表格中有四个不同的自定义 UITableView 单元格,我只想让一个单元格自行调整大小。

【问题讨论】:

  • 例如,您可以检查您的单元格索引路径,如果是 row == 0,则返回 UITableViewAutomaticDimension

标签: swift uitableview autolayout contentsize


【解决方案1】:

您必须知道要为哪些行实现UITableViewAutomaticDimension,因为当调用heightForRowAt 时,尚未创建任何单元格,因此您无法检查单元格类型,这就是您需要的原因改为检查indexPath.row

例如这样的:

switch indexPath.row {
case 0:
    return 100
case 1,2,3:
    return UITableViewAutomaticDimension
default:
    return 100
}

【讨论】:

  • @uzairshahzad,np。你改变了接受的答案,为什么我可以问。
  • 哎呀,对不起。那是错误的,纠正它。
【解决方案2】:

您可以检查您需要返回哪个单元格 dinamicHeigth 并在func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 中根据返回UITableViewAutomaticDimension

使用第 0 行作为条件的示例代码

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if(indexPath.row == 0)
        {
            return UITableViewAutomaticDimension
        }

        return 88
    }

【讨论】:

  • 你怎么知道 OP 想要第一个单元格?
  • 我只使用示例代码@RashwanL 条件可以是任意的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多