【问题标题】:UITableview with dynamic height cells & filling footer view具有动态高度单元格和填充页脚视图的 UITableview
【发布时间】:2017-05-17 21:09:08
【问题描述】:

我正在尝试使用填充 UITableview 剩余空白空间的页脚视图制作 UITableview。但是,问题是单元格的高度不同。

我知道,如果单元格都是静态高度,我可以简单地计算有多少个单元格并将其乘以静态高度。不幸的是,这在这种情况下不起作用。

目前的解决方案:

适用于静态高度单元格。

不适用于动态高度单元格。

    private func adjustFooterHeight() {
        if data.count == 0 {
            footerView.frame.size.height = tableView.bounds.height
        }
        else {
            //I use data.count instead of (data.count - 1) as I have the extra hidden cell, as pictured in image 2.
            let bottomFrame = tableView.rectForRow(at: IndexPath(row: data.count, section: 0))
            let height = tableView.bounds.height - (bottomFrame.origin.y + bottomFrame.height - rowHeight)
            footerView.frame.size.height = height < 0 ? 0 : height
            tableView.reloadData()
        }
    }

意向图:

【问题讨论】:

  • 您是想用一个非常自定义的视图来填充它,还是只想让 TableView 的其余部分成为纯色?例如白色?
  • 我不是特别希望视图有很多内容,但它需要让表格视图的内容到达视图控制器框架的底部。一旦表格视图有足够的单元格超过屏幕的大小,就可以了,我不需要它,但在那之前我需要它。我正在使用此页脚视图使表格视图可滚动,因为我将表格视图的第一个单元格隐藏在视图控制器上方。这有点像一个隐藏的下拉动作。我会用一两张图片更新我的帖子。
  • 背景图片会很棒:)
  • 您是否使用heightForRowAtIndexPath 实现了单元格高度?如果是这样,它不是完全动态的,您可以在 heightForFooterInSection 中定义页脚高度而无需重新加载数据。
  • 我正在为 heightForRowAtIndexPath 返回 UITableViewAutomaticDimensions,因为每个单元格的内容可能会根据它必须适应的文本量而有所不同。

标签: swift uitableview swift3


【解决方案1】:

所以我想通了……

这是解决方案。

    private func adjustFooterHeight() {

        //Get content height & calculate new footer height.
        let cells = self.tableView.visibleCells
        var height: CGFloat = 0
        for i in 0..<cells.count {
             height += cells[i].frame.height
        }
        height = self.tableView.bounds.height - ceil(height)

        //If the footer's new height is negative, we make it 0, since we don't need footer anymore.
        height = height > 0 ? height : 0

        //Create the footer
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: height))
        self.tableView.tableFooterView = footerView
    }

【讨论】:

  • 谢谢它似乎工作但你什么时候在生命周期中调用这个函数(adjustFooterHeight())?提前谢谢你。亲切的问候,
  • 你应该在调用函数 tableView.reloadData() 之后立即调用这个函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
相关资源
最近更新 更多