gpt4 book ai didi

ios - 如何使 tableViewCell Size 自动?

转载 作者:行者123 更新时间:2023-11-28 12:20:21 25 4
gpt4 key购买 nike

我有一个 table viewCell 并且有 4 个按钮,但在某些情况下我隐藏了一个或多个按钮以及如何自动调整 table view cell 的大小??? enter image description here

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionCell")!
variant1 = cell.contentView.viewWithTag(1) as! UIButton
variant2 = cell.contentView.viewWithTag(2) as! UIButton
variant3 = cell.contentView.viewWithTag(3) as! UIButton
variant4 = cell.contentView.viewWithTag(4) as! UIButton

let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
questionTextView.text = "\(Questions[indexPath.row].content!), подсказка: \(Questions[indexPath.row].hint!)"
variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside)
variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside)
variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside)
variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside)
//cell.contentView.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 0.65)

let numberOfVars = numberOfVariants[indexPath.row]
if (numberOfVars == 2) {
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.isHidden = true
variant4.isHidden = true
}
else if (numberOfVars == 3){
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
variant4.isHidden = true
}
else if (numberOfVars == 4) {
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
variant4.setTitle(Variants[3+amaunty[indexPath.row]].title, for: .normal)
}
return cell
}

最佳答案

在 UITableViewCell 中使用 UIStackView。在 stackview 中添加您的按钮并为 stackview 提供所有四个约束,不要给出固定的高度宽度。现在,在行索引的单元格中,根据您的情况隐藏您的任何按钮。

此代码用于 UITableViewCell 的动态高度。

extension ViewController : UITableViewDelegate,UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ButtonCell

switch indexPath.row {
case 1:
cell.btn1.isHidden = true
break
case 2:
cell.btn2.isHidden = true
break
case 3:
cell.btn3.isHidden = true
cell.btn2.isHidden = true
break
case 4:
cell.btn4.isHidden = true
cell.btn3.isHidden = true
cell.btn2.isHidden = true
break
default:
print("Default")
}

return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}

第一个屏幕截图向您展示了 stackview 控件的属性。

enter image description here

第二个屏幕截图显示了给堆栈 View 的所有四个约束以及给所有按钮的高度约束。

enter image description here

最终回应

enter image description here

谢谢

关于ios - 如何使 tableViewCell Size 自动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45079196/

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