我有一個高度表視圖的問題,例如,如果我在表視圖中有 3 行,每行有 50 ,表視圖將為 150(我在表視圖中有一個表視圖,問題在第二個表中),對于兩個表格視圖單元格,我從 xib 加載了一個自定義單元格,我在表格視圖單元格中附加了一個 xib
這是我的第一個表格視圖
這是第一個表視圖的xib,第二個表視圖在里面,我設定的底部關系大于或等于
這是第一個視圖代碼:
class QuestionList: UIView{
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableViewHC: NSLayoutConstraint!
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
setTableView()
}
func commonInit(){
let viewFromXib = Bundle.main.loadNibNamed("QuestionList", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
questionLabel.normalGray()
addSubview(viewFromXib)
}
func setTableView(){
tableView.delegate = self
tableView.dataSource = self
let nib = UINib(nibName: "AnswerListTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "answerListCell")
tableView.isScrollEnabled = false
tableView.rowHeight = UITableView.automaticDimension
tableViewHC.constant = CGFloat(150 * 3)
}
}
extension QuestionList: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "answerListCell", for: indexPath) as! AnswerListTableViewCell
cell.separatorInset.right = cell.separatorInset.left
cell.answerList.optionLabel.text = "Text Text"
return cell
}
}
這是第二個表格視圖的第二個 xib
這是第二個視圖代碼:
class AnswerList: UIView {
@IBOutlet weak var optionSwitch: UISwitch!
@IBOutlet weak var optionLabel: UILabel!
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit(){
let viewFromXib = Bundle.main.loadNibNamed("AnswerList", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
optionLabel.normalGray()
addSubview(viewFromXib)
}
}
這是我的輸出:

uj5u.com熱心網友回復:
您想要實作更高的 tableView 來解決這個問題嗎?如果是這樣,您可能可以將 tableView 的 heightEquals 更改為大于當前 = 160 的值。
您還可以計算并將 tableView 設定為不同的高度,如下所示:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
uj5u.com熱心網友回復:
如果您希望 tableview 高度是它的內容,請計算 viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
tabelView.height = tableView.contentSize.height
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/396573.html
