[英]Table view width base on number rows issue
我对高度表视图有疑问,例如,如果我在表视图中有 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)
}
}
这是我的 output:
更高的 tableView 是您想要解决的问题吗? 如果是这样,您可能可以将 tableView 的 heightEquals 更改为大于当前 = 160 的值。
您还可以计算并将 tableView 设置为不同的高度,如下所示:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
如果您希望 tableview 高度成为它的内容,请在viewDidLayoutSubviews
中计算
override func viewDidLayoutSubviews() {
tabelView.height = tableView.contentSize.height
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.