swift - 具有动态高度单元格和填充页脚 View 的 UITableview

标签 swift uitableview swift3

我正在尝试制作一个带有页脚 View 的 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()
        }
    }

意图图:

enter image description here

最佳答案

所以我想通了...

这是解决方案。

    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
    }

关于swift - 具有动态高度单元格和填充页脚 View 的 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43969415/

相关文章:

ios - 选择时的 UICollectionView 动画单元格图像

iphone - 从 Nib 文件加载自定义表格 View 单元格

ios - 如何通过页脚单元格在表格 View 中使用委托(delegate)?

swift3 - 通过 cocoapods 安装领域时出错

swift - 将重载的 += 运算符声明为变异?

ios - SQlite密码IOS

ios - Swift 3 - 用户通知自定义重复像 iOS 提醒应用程序

iOS 实时数据进程间通信

objective-c - 大 tableview 滚动滞后

ios - 将节点设置为从一系列点随机生成并以逐渐加快的速度下降