【问题标题】:How to use dynamic height of UITableViewCell in auto-layout and move other views to up when bottom view is hidden?如何在自动布局中使用 UITableViewCell 的动态高度并在底部视图隐藏时将其他视图向上移动?
【发布时间】:2020-01-18 06:05:14
【问题描述】:

我在 xib 中有一个 UITableViewCell,它在相应的 UITableViewCell 子类中有一个出口。我从

返回单元格的高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {

return 400

}

我需要根据表格每一行中可用的数据隐藏一些视图,并且底部视图应移动到单元格的顶部。当我从单元格中隐藏视图时,会留下空白空间来代替隐藏视图,并且底部视图不会移动到单元格的顶部。

这是我如何隐藏单元格视图。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    .....
    cell.opetion4.isHidden = true
    cell.opetion3.isHidden = true

}

这是我的牢房。

隐藏 2 个中间标签后如下所示。

但我想删除这个空白空间,并希望将底部标签移到顶部,如下所示。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    首先将UITableViewCell的高度设为UITableView.automaticDimension

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

    将所有问题标签嵌入到 UIStackView(垂直)中,不包括 bottomLabel。在UIStackViewbottomLabel 之间设置AutoLayoutConstraint

    UILabels 的numberOfLines 属性设置为0(零)。

    UIStackView分布设置为Fill

    然后,在您的 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 方法中隐藏标签。它会自动处理UILabels之间的空格

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
    
        cell.questionLabel1.text = labelOneText[indexPath.row]
        cell.questionLabel2.text = labelTwoText[indexPath.row]
        cell.questionLabel3.text = labelThreeText[indexPath.row]
    
        if labelOneText[indexPath.row] == "" {
            cell.questionLabel1.isHidden = true
        }
    
        if labelTwoText[indexPath.row] == "" {
            cell.questionLabel2.isHidden = true
        }
    
        if labelThreeText[indexPath.row] == "" {
            cell.questionLabel3.isHidden = true
        }
    
        return cell
    }
    

    最终输出:

    【讨论】:

      【解决方案2】:

      首先我建议您将 UITableViewCell 高度设置为 automatic dimensions 。将所有孩子彼此连接,最后一个孩子连接到 xib 的uiview。现在隐藏视图不会调整单元格的大小,因此您需要使用 uiview 您正在隐藏的高度约束。

      IBOutlet 中使高度约束同样强,否则它将崩溃,因为单元格正在重复使用,并且设置一次后constraint 将变为nil。您需要确保根据显示单元格要求更改高度约束,这意味着每个cell 都维护一些datasource,决定每次调用cellforrowatIndexpath 方法时显示或隐藏视图。

      希望对你有帮助

      【讨论】:

        【解决方案3】:
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {
        
        return UITableView.automaticDimension
        
        }
        

        现在在单元格中,将您的所有视图和您想要隐藏/显示的兄弟姐妹放在UIstackview (horizontal) 中。现在如果你隐藏一个视图,它将被隐藏,它的速度也将被隐藏,不会显示任何空白,也不需要处理额外的约束。全部由stackview处理。

        【讨论】:

          猜你喜欢
          • 2013-08-06
          • 2014-04-17
          • 1970-01-01
          • 2023-03-05
          • 2014-05-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-18
          相关资源
          最近更新 更多