簡體   English   中英

基於行數問題的表格視圖寬度

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM