我应该何时重新计算UITableView页脚的大小?

时间:2018-10-04 13:32:56

标签: ios swift uitableview

我想根据表格视图内容大小的变化重新计算表格视图页脚的高度。当表格的行为零时,页脚的高度将为最大。当将行添加到表中时,页脚的高度将减小,直到达到最小值。我正在做的是使用页脚来填充零行或几行时表格底部的空白区域。除了添加行之外,由于现有行的高度(内容)已更改,因此内容大小也可能会更改。

假设我有一个视图控制器,其主视图包含两个子视图:一个按钮视图和一个表视图。单击该按钮将导致修改数据存储并调用表的reloadData方法。我何时/在何处为表的tableFooterView.bounds.size.height分配新值?

我还应该指出,我正在使用UITableViewAutomaticDimension。如果在表的数据源委托方法cellForRowAt中,我打印出得到的像元高度:

Upper table cell height = 21.0
Upper table cell height = 21.0
Upper table cell height = 21.0
Upper table cell height = 21.0
Upper table cell height = 44.0

除了最后一个,新的,所有21个。这一定是由于尚未应用自动尺寸标注。

更新:

我已经初步找到了以下解决方案(非常感谢this线程上的所有人员为解决方案的最大一部分)。我是暂定的,因为该解决方案涉及两次调用reloadData以便处理contentSize的问题。有关contentSize问题的演示,请参见this GitHub项目。

class TableView: UITableView {

    override func reloadData() {
        execute() { super.reloadData() }
    }

    override func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) {
        execute() { super.reloadRows(at: indexPaths, with: animation) }
    }

    private func execute(reload: @escaping () -> Void) {
        CATransaction.begin()
        CATransaction.setCompletionBlock() {
            if self.adjustFooter() {
                reload() // Cause the contentSize to update (see GitHub project)
                self.layoutIfNeeded()
            }
        }
        reload()
        CATransaction.commit()
    }

    // Return true(false) if the footer was(was not) adjusted
    func adjustFooter() -> Bool {
        guard let currentFrame = tableFooterView?.frame else { return false }

        let newHeight = calcFooterHeight()
        let adjustmentNeeded = newHeight != currentFrame.height

        if adjustmentNeeded {
            tableFooterView?.frame = CGRect(x: currentFrame.minX, y: currentFrame.minY, width: currentFrame.width, height: newHeight)
        }

        return adjustmentNeeded
    }

    private let minFooterHeight: CGFloat = 44
    private func calcFooterHeight() -> CGFloat {
        guard let footerView = tableFooterView else { return 0 }

        let spaceTaken = contentSize.height - footerView.bounds.height
        let spaceAvailable = bounds.height - spaceTaken
        return spaceAvailable > minFooterHeight ? spaceAvailable : minFooterHeight
    }
}

2 个答案:

答案 0 :(得分:0)

UITableViewDelegate具有方法tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat,我们可以使用该方法指定节脚的高度。当我们为表视图调用reloadData()或更改屏幕方向等时,此方法会触发。

因此您可以实现此方法来计算页脚的新高度:

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    guard section == 0 else { return 0.0 } // assume there is only one section in the table

    var cellsHeight: CGFloat = 0.0

    let rows = self.tableView(tableView, numberOfRowsInSection: section)

    for row in 0..<rows
    {
        let indexPath = IndexPath(item: row, section: section)
        cellsHeight += self.tableView(tableView, heightForRowAt: indexPath)
    }

    let headerHeight: CGFloat = tableView.tableHeaderView?.frame.height ?? 0.0
    let footerHeight = view.frame.height - headerHeight - cellsHeight

    return footerHeight
}

答案 1 :(得分:0)

我得出以下解决方案。非常感谢this线程上的所有人员,他们为解决方案提供了最大的帮助。 TableViewController.TableView类提供所需的功能。其余代码完善了一个完整的示例。

//
//  TableViewController.swift
//  Tables
//
//  Created by Robert Vaessen on 11/6/18.
//  Copyright © 2018 Robert Vaessen. All rights reserved.
//
//  Note: Add the following to AppDelegate:
//
//    func application(_ application: UIApplication,
//                    didFinishLaunchingWithOptions launchOptions: 
//                    [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//        window = UIWindow(frame: UIScreen.main.bounds)
//        window?.makeKeyAndVisible()
//        window?.rootViewController = TableViewController()
//        return true
//    }


import UIKit

class TableViewController: UIViewController {

    class TableView : UITableView {

        override func reloadData() {
            execute() { super.reloadData() }
        }

        override func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) {
            execute() { super.reloadRows(at: indexPaths, with: animation) }
        }

        private func execute(reload: @escaping () -> Void) {
            CATransaction.begin()
            CATransaction.setCompletionBlock() {
                print("Reload completed")
                _ = self.adjustFooter()
            }
            print("\nReload begun")
            reload()
            CATransaction.commit()
        }

        private func adjustFooter() -> Bool {
            guard let footerView = tableFooterView else { return false }

            func calcFooterHeight() -> CGFloat {
                var heightUsed = tableHeaderView?.bounds.height ?? 0
                for cell in visibleCells { heightUsed += cell.bounds.height }
                let heightRemaining = bounds.height - heightUsed

                let minHeight: CGFloat = 44
                return heightRemaining > minHeight ? heightRemaining : minHeight
            }

            let newHeight = calcFooterHeight()
            guard newHeight != footerView.bounds.height else { return false }

            // Keep the origin where it is, i.e. tweaking just the height expands the frame about its center
            let currentFrame = footerView.frame
            footerView.frame = CGRect(x: currentFrame.origin.x, y: currentFrame.origin.y, width: currentFrame.width, height: newHeight)

            return true
        }
    }

    class FooterView : UIView {
        override func draw(_ rect: CGRect) {
            print("Drawing footer")
            super.draw(rect)
        }
    }

    private var tableView: TableView!

    private let cellReuseId = "TableCell"
    private let data: [UIColor] = [UIColor(white: 0.4, alpha: 1), UIColor(white: 0.5, alpha: 1), UIColor(white: 0.6, alpha: 1), UIColor(white: 0.7, alpha: 1)]
    private var dataRepeatCount = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        func createTable(in: UIView) -> TableView {

            let tableView = TableView(frame: CGRect.zero)

            tableView.separatorStyle = .none
            tableView.translatesAutoresizingMaskIntoConstraints = false

            `in`.addSubview(tableView)

            tableView.centerXAnchor.constraint(equalTo: `in`.centerXAnchor).isActive = true
            tableView.centerYAnchor.constraint(equalTo: `in`.centerYAnchor).isActive = true
            tableView.widthAnchor.constraint(equalTo: `in`.widthAnchor, multiplier: 1).isActive = true
            tableView.heightAnchor.constraint(equalTo: `in`.heightAnchor, multiplier: 0.8).isActive = true

            tableView.dataSource = self
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseId)

            return tableView
        }

        func addHeader(to: UITableView) {
            let header = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
            to.tableHeaderView = header

            let color = UIColor.black
            let offset: CGFloat = 64

            let add = UIButton(type: .system)
            add.setTitle("Add", for: .normal)
            add.layer.borderColor = color.cgColor
            add.layer.borderWidth = 1
            add.layer.cornerRadius = 5
            add.tintColor = color
            add.contentEdgeInsets = UIEdgeInsets.init(top: 8, left: 8, bottom: 8, right: 8)
            add.addTarget(self, action: #selector(addRows), for: .touchUpInside)
            add.translatesAutoresizingMaskIntoConstraints = false
            header.addSubview(add)
            add.centerXAnchor.constraint(equalTo: to.centerXAnchor, constant: -offset).isActive = true
            add.centerYAnchor.constraint(equalTo: header.centerYAnchor).isActive = true

            let remove = UIButton(type: .system)
            remove.setTitle("Remove", for: .normal)
            remove.layer.borderColor = color.cgColor
            remove.layer.borderWidth = 1
            remove.layer.cornerRadius = 5
            remove.tintColor = color
            remove.contentEdgeInsets = UIEdgeInsets.init(top: 8, left: 8, bottom: 8, right: 8)
            remove.addTarget(self, action: #selector(removeRows), for: .touchUpInside)
            remove.translatesAutoresizingMaskIntoConstraints = false
            header.addSubview(remove)
            remove.centerXAnchor.constraint(equalTo: header.centerXAnchor, constant: offset).isActive = true
            remove.centerYAnchor.constraint(equalTo: header.centerYAnchor).isActive = true
        }

        func addFooter(to: UITableView) {
            let footer = FooterView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
            footer.layer.borderWidth = 3
            footer.layer.borderColor = UIColor.red.cgColor
            //footer.contentMode = .redraw
            to.tableFooterView = footer
        }

        tableView = createTable(in: view)
        addHeader(to: tableView)
        addFooter(to: tableView)

        view.backgroundColor = .white
        tableView.backgroundColor = .black // UIColor(white: 0.2, alpha: 1)
        tableView.tableHeaderView!.backgroundColor = .cyan // UIColor(white: 0, alpha: 1)
        tableView.tableFooterView!.backgroundColor = .white // UIColor(white: 0, alpha: 1)
    }

    @objc private func addRows() {
        dataRepeatCount += 1
        tableView.reloadData()
    }

    @objc private func removeRows() {
        dataRepeatCount -= dataRepeatCount > 0 ? 1 : 0
        tableView.reloadData()
    }
}

extension TableViewController : UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard section == 0 else { fatalError("Unexpected section: \(section)") }
        return dataRepeatCount * data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseId, for: indexPath)

        cell.textLabel?.textAlignment = .center
        cell.backgroundColor = data[indexPath.row % data.count]
        cell.textLabel?.textColor = .white
        cell.textLabel?.text = "\(indexPath.row)"

        return cell
    }
}