【问题标题】:Set TableView height by the number or rows通过数字或行设置 TableView 高度
【发布时间】:2023-03-27 17:19:02
【问题描述】:

我在MainstoryBoard 中有TableView,并且每次的行数都是随机的。 我希望整个 TableView 的高度是灵活的 - 我的意思是,例如:如果我在 TableView 中有 4 行并且每个 TableView 行高度为 22,那么 TableView 高度将为 88 . 另一个例子: 行数:2 行高 = 22 TableView 将是 44。

我该怎么做?

【问题讨论】:

    标签: ios swift xcode uitableview cocoa-touch


    【解决方案1】:

    请找到下面的代码。

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UIScreen.main.bounds.height
    }
            
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let tableViewHeight = self.tableView.frame.height
        let contentHeight = self.tableView.contentSize.height
                
        let centeringInset = (tableViewHeight - contentHeight) / 2.0
        let topInset = max(centeringInset, 0.0)
                
        self.tableView.contentInset = UIEdgeInsets(top: topInset, left: 0.0, bottom: 0.0, right: 0.0)
    }
    

    【讨论】:

      【解决方案2】:

      这里没有什么是我最终使用的解决方案,它给出了准确的高度。

      extension UITableView {
          var contentSizeHeight: CGFloat {
              var height = CGFloat(0)
              for section in 0..<numberOfSections {
                  height = height + rectForHeader(inSection: section).height
                  let rows = numberOfRows(inSection: section)
                  for row in 0..<rows {
                      height = height + rectForRow(at: IndexPath(row: row, section: section)).height
                  }
              }
              return height
          }
      }
      

      用法:

      tableView.contentSizeHeight
      

      将为您提供表格视图内容的实际计算高度。

      【讨论】:

        【解决方案3】:

        取出tableview高度的出口,并在cellForRowAt中使用您的tableview行高设置它,

        tableviewHeight.constant = (tableView.contentSize.height * no_of_rows) + bottom_space

        【讨论】:

          【解决方案4】:

          只需取出tableview高度约束的出口并在UITableViewwillDisplay cell: UITableViewCell方法中更新它

          @IBOutlet weak var tblHConstraint: NSLayoutConstraint!
          
          
          func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                  self.tblHConstraint.constant = self.tblView.contentSize.height
              }
          

          【讨论】:

            【解决方案5】:

            注意:当您增加 tableview 高度时,它会超出视图。你会遇到滚动问题。

            例如:在保持 tableview 的内部获取一个视图,并将左、右、下、上的约束设置为零(0)。

            现在重新加载 tableview 假设 2 行,每行高度为 20,现在总行高为 40。它很好并将这些高度提供给 table view,表格视图将仅显示 40 高度(您可以通过约束来分配 tableview 高度(或)表格视图内容大小都根据行高给出相同的高度)。

            但是,如果您重新加载 50 行,您将遇到滚动问题,因为这些行的高度赋予了表格视图高度,这里表格视图高度比您的屏幕高度扩展得更多。

            要解决这个问题,你应该在 tableview 之外使用滚动视图。

            【讨论】:

              【解决方案6】:

              与自动布局一起使用:

              viewDidLoad()某处

              tableView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor)
              

              然后在你的viewDidLayoutSubViews()

              tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true
              

              【讨论】:

              • 为什么这不是最佳答案?你拯救了我的一天!
              • 谢谢伙计。我在代码中设置了故事板的顶部、尾随、前导和高度。为我工作
              【解决方案7】:
              DispatchQueue.main.async {
                  var frame = tableView.frame
                  frame.size.height = tableView.contentSize.height
                  tableView.frame = frame
              }
              

              DispatchQueue.main.async {
                  self.TblViewHeightConstraint.constant = CGFloat((self.array.count) * 30)//Here 30 is my cell height
                  self.TblView.reloadData()
              }
              

              【讨论】:

              • 如何获取单元格高度而不是将其静态设置为 30?
              【解决方案8】:

              您可以根据 contentSize 改变 UITableView 的高度,如下所示:

              斯威夫特 2.2

              tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height)
              

              Swift 3 或 4+

              tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
              

              并确保在viewDidAppear 方法中写入上述行

              你还需要在viewDidLayoutSubviews中写上上面的那行。

              斯威夫特 2.2

              func viewDidLayoutSubviews(){
                   tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height)
                   tableView.reloadData()
              }
              

              Swift 3 或 4+

              func viewDidLayoutSubviews(){
                   tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
                   tableView.reloadData()
              }
              

              【讨论】:

              • 改变 UITableView 的高度。例如,如果我有 2 行,每行的高度为 22,那么 UITableView 的高度将为 44
              • 如果你想动态改变 UITableView 的高度试试这个,它可能对你有帮助。 stackoverflow.com/questions/18784741/…
              • 索希尔,谢谢,它有效。但我遇到了另一个问题 - 我的 TableView 在 ScrollView 中,所以每次我上下移动屏幕(不是 tableView)时,tableView 的高度都会更改为默认值,而不是 contentSize。
              • @SohilR.Memon 试过了,不行..(顺便把它改成覆盖函数)
              • @SohilR.Memon:如何使用 Autolayout 增加 tableView 高度?
              【解决方案9】:

              在 Swift 3 中使用它

               override func viewDidLayoutSubviews(){
                  tableView.frame = CGRect(x: tableView.frame.origin.x, y:  tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
                  tableView.reloadData()
              }
              

              【讨论】:

              • 我很困惑...为什么在加载数据之前更新框架?
              【解决方案10】:

              取出父视图的Height Constraint,赋值为table view内容的高度+常量(视图中其他内容的额外高度)

              heightConstraintView.constant = tableView.contentSize.height + constantValue //use the value of constant as required.
              

              并在 cellForRowAt 方法中编写此代码。

              【讨论】:

              • 这似乎是对 Memon 先生的更好回答。每当我在现代 iOS 中设置框架时,我都觉得自己做错了什么。也许 cellForRow 不是最适合它的地方 - 当然它是一个可靠的地方。
              • 可以,但是如果tableview有20个cell,一次全部初始化,没有延迟加载...怎么实现呢?
              • 我必须在 DispatchQueue.main.async 块中完成它,然后它就起作用了。谢谢
              猜你喜欢
              • 1970-01-01
              • 2016-10-19
              • 1970-01-01
              • 2012-06-18
              • 2021-09-18
              • 1970-01-01
              • 1970-01-01
              • 2023-03-23
              • 1970-01-01
              相关资源
              最近更新 更多