【问题标题】:Find height of the empty space in a UITableView在 UITableView 中查找空白空间的高度
【发布时间】:2011-10-10 10:55:04
【问题描述】:

在我的 UITableView 中,我需要将 UIView 放置在表格的页脚和屏幕之外。例如,“Push to refresh”功能(如 Twitter 应用程序)在表格的乞求处和屏幕之外,我需要相同但在底部。

如果我有足够的总高度大于屏幕高度的行,我没有任何问题,但如果我没有任何行,我无法定位 muy 视图。

所以我需要在表格中添加一个空白单元格(自定义单元格)来填充最后一个单元格和屏幕底部之间的空白,我该怎么做?我怎么知道空白的高度?

非常感谢您的帮助。

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    为什么不使用相同的表格页脚视图而不是空白单元格,并使其足够大以容纳该空白空间。好像容易多了。

    现在,我能想到的唯一解决方案是手动计算内容的高度。为此,我认为您可以使用技巧并调用

    
    CGRect lastRowFrame = [tableView rectForRowAtIndexPath: last_row_index_path];
    

    正如文档所说,这将为您提供最后一行的框架,并且您可以计算frame.size.height 和最后一行框架之间的差异。像这样的:

    
    CGFloat emptySpaceHeight = tableView.frame.size.height - (lastRowFrame.origin.y + lastRowFrame.size.height);
    

    这将是您需要添加到页脚视图的空白区域。

    哦,还有最后一个获得这种效果的技巧:确保使用tableView.contentInset 将底部插图设置为减去“推送刷新”消息高度。这将使 tableview 反弹并随着消息离开屏幕而休息。

    现在我想不出更好的方法来实现你想要的。所以,希望这会有所帮助。

    【讨论】:

    • 这对我来说是崩溃的......有点像无限循环。我在 viewForFooterInSection 方法和 heightForFooterInSection 方法中进行空白计算。我认为问题是我试图在初始化之前找到一个部分的最后一个 indexPath。
    【解决方案2】:

    斯威夫特版本

            lastRowFrame = tableView.rectForRow(at: indexPath)
            let emptySpaceHeight = tableView.frame.size.height - (lastRowFrame.origin.y + lastRowFrame.size.height)
    

    【讨论】:

      【解决方案3】:

      您可以尝试将其添加到 heightforrowat 函数中,这可以让底部单元格具有最后一个单元格和屏幕底部之间缺失空间的大小。

          if indexPath == (lastIndexPath) {
              var currentCellsHeight: CGFloat = 0
              let minimumFooterCellHeight = 50 // Whatever you want the minimum to be
      
              for visibleCell in tableView.visibleCells {
                  currentCellsHeight += visibleCell.frame.height
              }
      
              if let footerCell = tableView.cellForRow(at: indexPath) {
                  currentCellsHeight -= footerCell.frame.height
              }
              if self.view.frame.height - currentCellsHeight < minimumFooterCellHeight {
                  return minimumFooterCellHeight
              } else {
                  return self.view.frame.height - currentCellsHeight
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多