【问题标题】:TableView inside ScrollView , How to calculate tableview height at runtime?ScrollView 中的 TableView ,如何在运行时计算 tableview 高度?
【发布时间】:2020-11-26 09:51:54
【问题描述】:

滚动视图中有表格视图,为了完美滚动需要表格视图的高度。但是单元格的高度和单元格中的多个内容具有动态数据,例如图像(使用翠鸟库计算图像的高度)和内容(行数为 0)。所以无法计算每个单元格的高度。所以我用它来获取单元格的高度:-

let totalCount = self.itemArray.data1.count + self.itemArray.data2.count
   if totalCount != self.totalHeightOfTable.count {
      //Appending height of cell
       self.tableView.reloadData()
       self.totalHeightOfTable.append(cell.frame.height)
       self.heightOfPost = self.totalHeightOfTable
       if totalCount == self.totalHeightOfTable.count {
          // Call back to get height of tableView
           self.getTotalHeightOfTableView?(self.totalHeightOfTable)
        }
  } 

因为 tableView 位于 scrollview 内,我无法动态或在运行时计算 tableView 的每个单元格的高度。我在运行时得到的高度更大,并且在 tableView 的末尾有一个空白区域。所以表格视图的总高度总是大于表格视图中所有单元格的总和。

UI structure attached

【问题讨论】:

    标签: ios swift iphone uitableview uiscrollview


    【解决方案1】:

    我了解您的问题,您想计算 动态表格 高度 灵活的单元格高度 >tableview 并根据这个高度你想更新你的父滚动视图 contentSize 高度。

    我想告诉你,请删除你所有的高度计算,只需将这个简单函数下方放在你的视图控制器旁边。

    重要提示:-如果您使用的是AutoLayout,请不要为您的表格视图提供任何高度约束从您的故事板或以编程方式。

    请注意您的 tableviewscrollview 变量名并分别替换它们。

       //MARK:- viewDidLayoutSubviews will call after dynamic height calculation automatically
    override func viewDidLayoutSubviews() {
        
        //isScrollEnabled of table view  should be dissable because our table is inside scrollview.
        tableView.isScrollEnabled = false
        
        
        //if above tableView.contentSize.height   not zero and giving acurate value then proceed further and update our parent scroll view contentsize height for height.
        print(tableView.contentSize.height)
        
        
        
        //place some bottom peeding as you want
        let bottomPedding:CGFloat = 30
        
        
        //Finally update your scrollview content size with newly created table height + bottom pedding.
        scrollview.contentSize = CGSize.init(width: scrollview.contentSize.width, height:tableView.contentSize.height + bottomPedding)
        
        
    }
    

    如果这行得通,那就太棒了,那就太好了,休息时,您可以随时与我联系,我们会解决的。 asrathoreforiphone@gmail.com

    【讨论】:

    • 我想要在表格视图单元格中使用相同的东西 就像目前在表格单元格中一样,单元格中还有一个表格,我想要表格视图高度(在单元格中)。对此有任何建议
    • 好的,给我一些时间会在视频中为你解释。休息一下,您可以通过我的电子邮件 asrathoreforiphone@gmail.com 联系我
    【解决方案2】:

    你可以通过taleView.contentSize.height使用contentSize的高度

    【讨论】:

      【解决方案3】:

      欢迎来到 Stackoverflow!

      您绝对可以获取contentSize,并从中获取tableView 的高度。我一直在使用这种方法并且每次都有效。

      一种方法是添加观察者,如下所示:

      tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
      

      然后覆盖控制器的方法observeValue,如下所示:

      override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
          if let obj = object as? UITableView {
              if obj == self.tableView && keyPath == "contentSize" {
                  if let newSize = change?[NSKeyValueChangeKey.newKey] as? CGSize {
                      let height = newSize.height // <----- your height!
                  }
              }
          }
      }
      

      添加,也许最好在您的 viewWillDisappeardeinit 方法中删除该观察者。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-28
        • 2013-01-28
        • 1970-01-01
        • 2019-05-23
        • 2016-04-21
        • 1970-01-01
        • 2021-09-26
        • 1970-01-01
        相关资源
        最近更新 更多