我有一個表viewCell,並有4個按鈕,但在某些情況下,我隱藏了一個或多個按鈕,以及如何使表視圖單元格的大小自動? 如何使tableViewCell Size自動?
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
}
是這個單元被設計成在故事板或動態細胞靜細胞? –
在按鈕上添加高度約束併爲約束創建出口。如果要隱藏某個按鈕,只需使用其相應的高度約束出口將其高度設置爲0即可。如果需要更多幫助,請同時使用此按鈕。https://www.raywenderlich。 com/129059/self-sizing-table-view-cells – sanman
在單元格中使用StackView並使表格視圖單元高度動態化。當您隱藏按鈕時,堆疊視圖將自動管理高度。 – Ujesh