【问题标题】:How can I change the height of one static cell | Swift如何更改一个静态单元格的高度 |迅速
【发布时间】:2021-02-04 15:08:44
【问题描述】:

基本上我想在满足两个条件时在我的静态表中隐藏一个单元格:

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

    var height: CGFloat
    if indexPath.row == 1 && unitImage == nil {
        height = 0
    } else {
     height = 190
        }
    
    return height
}

问题是每个单元格在情节提要中设置了不同的高度,如果 unitImage 为空白,我真的只需要隐藏第一行。

【问题讨论】:

  • 你能解释一下这个吗...故事板中每个单元格的高度不同是什么意思?你不是在运行时指定高度吗?
  • 通常静态单元格由插座驱动,而不是由数据源和委托驱动。

标签: swift


【解决方案1】:

在我的代码中,我喜欢下面的代码

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

        if selectedIndex == indexPath.row  {
            return 288
        } else {
            return 56
        }
 
    } else if indexPath.row == 1 {
        
        if selectedIndex == indexPath.row {
            return 160
        } else {
            return 56
        }
 
    } else if indexPath.row == 2 {
        
        if selectedIndex == indexPath.row  {
            return 1576
        } else {
            return 56
        }
 
    } else {
        return 0
    }
}

选择的索引是我选择的单元格索引,所以请根据您的要求进行更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多