gpt4 book ai didi

ios - 如何在自动布局中使用 UITableViewCell 的动态高度并在隐藏底部 View 时将其他 View 向上移动?

转载 作者:行者123 更新时间:2023-12-02 00:02:17 26 4
gpt4 key购买 nike

我在 xib 中有一个 UITableViewCell ,其导出在相应的 UITableViewCell 子类中。我正在从

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

return 400

}

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

这是我隐藏单元格 View 的方法。

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

}

这是我的手机。

enter image description here

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

enter image description here

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

enter image description here

最佳答案

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

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

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

enter image description here

UILabelnumberOfLines 属性设置为 0(零)。

enter image description here

UIStackViewDistribution设置为Fill

enter image description here

然后,在您的 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
}

最终输出:

enter image description here

关于ios - 如何在自动布局中使用 UITableViewCell 的动态高度并在隐藏底部 View 时将其他 View 向上移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59797980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com