MAD*_*ARI 7 uitableview ios swift
我在 xib 中有一个 UITableViewCell ,其出口在相应的 UITableViewCell 子类中。我正在返回单元格的高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {
return 400
}
Run Code Online (Sandbox Code Playgroud)
我需要根据表格每行中可用的数据隐藏一些视图,底部视图应移至单元格顶部。当我从单元格隐藏视图时,隐藏视图的位置会留下空白空间,底部视图不会转移到单元格的顶部。
这是我隐藏单元格视图的方法。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
.....
cell.opetion4.isHidden = true
cell.opetion3.isHidden = true
}
Run Code Online (Sandbox Code Playgroud)
这是我的牢房。
隐藏 2 个中间标签后,它看起来如下。
但我想删除这个空白空间,并希望将底部标签移到顶部,如下所示。
首先,将UITableViewCell的高度设置为UITableView.automaticDimension
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Run Code Online (Sandbox Code Playgroud)
将所有问题标签嵌入UIStackView(垂直)排除中bottomLabel。将AutoLayoutConstraint设置在UIStackView和之间bottomLabel。
将snumberOfLines的属性设置UILabel为 0(零)。
将分布UIStackView设置为Fill
然后,在您的tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell方法中隐藏标签。UILabel它会自动处理s之间的空格
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
}
Run Code Online (Sandbox Code Playgroud)
最终输出: