在 uitableview 底部放置一个单元格

WKL*_*WKL 5 uitableview ios

如何在 UITableView 底部放置“cellBOTTOM”?我想过使用表格视图页脚,但这会导致如果没有足够的单元格来填满整个屏幕,页脚不会粘在屏幕底部,而是正好在“cellLAST”的正下方。我需要“cellBOTTOM”位于屏幕底部和表格底部

+--------------------+    +--------------------+    +--------------------+
|       SCREEN       |    |       SCREEN       |    |       SCREEN       |  
| +----------------+ |    | +----------------+ |    | +----------------+ |
| |     TABLE      | |    | |     TABLE      | |    | |     TABLE      | |
| | +------------+ | |    | | +------------+ | |    | | +------------+ | |
| | |  cellLAST  | | |    | | |   cell 1   | | |    | | |   cell 1   | | |
| | +------------+ | |    | | +------------+ | |    | | +------------+ | |
| |                | |    | | +------------+ | |    | | +------------+ | |
| |                | |    | | |  cellLAST  | | |    | | |   cell 2   | | |
| |                | |    | | +------------+ | |    | | +------------+ | |
| |                | |    | |                | |    | | +------------+ | |
| |                | |    | |                | |    | | |   cell 3   | | |
| |                | |    | |                | |    | | +------------+ | |
| | +------------+ | |    | | +------------+ | |    | | +------------+ | |
| | | cellBOTTOM | | |    | | | cellBOTTOM | | |    | | |   cell 4   | | |
| | +------------+ | |    | | +------------+ | |    | | +------------+ | |
| +----------------+ |    | +----------------+ |    | | +------------+ | |
+--------------------+    +--------------------+    +-+ |  cellLAST  | +-+
                                                      | +------------+ |
                                                      | +------------+ |
                                                      | | cellBOTTOM | |
                                                      | +------------+ |
                                                      +----------------+
Run Code Online (Sandbox Code Playgroud)

Mar*_*cus 0

我在午餐时快速浏览了一遍。我最初认为这可以通过重写 ScrollViewDidScroll 来完成,但无法以这种方式解决。我想出的答案是动态决定表格是否应显示页脚视图。

在下面的代码中(虽然很粗糙,但应该能让您有所了解),我们测试表的行数是否大于我们想要在屏幕上显示的行数(在本例中,我已将其硬编码为 10)。如果是,那么我们从源数据数组的最后一个元素创建一个页脚,并将页脚高度设置为与其他单元格相同的高度,并具有不同的背景颜色。如果不是,那么我们使用 cellForItemAtIndexPath 来测试我们是否位于数据源的最后一行,在这种情况下,我们相应地格式化单元格:

import UIKit

class ViewController: UIViewController {

    var myTrackingTableViewController : TrackingTableViewController!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let sourceData : [String] = {
            var sD : [String] = [String]()
            for count in 0..<50 {            // Use the top end of this loop to configure the number of rows in the tableview
                sD.append("Number \(count)")
            }
            return sD
        }()

        myTrackingTableViewController = TrackingTableViewController(sourceData: sourceData, numberOfRowsOnScreen: 10, style: UITableViewStyle.plain)

        self.view.addSubview(myTrackingTableViewController.tableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class TrackingTableViewController : UITableViewController {

    let sourceData : [String]
    var footerText : String!
    var footerInline : Bool = false

    init(sourceData: [String], numberOfRowsOnScreen: Int, style: UITableViewStyle) {
        self.sourceData = sourceData
        super.init(style: style)

        if self.sourceData.count > numberOfRowsOnScreen {
            self.footerText = sourceData.last
        }
        else
        {
            footerInline = true
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if footerInline {
            return sourceData.count
        }
        return sourceData.count - 1
    }

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if footerText != nil {
            return self.tableView.frame.height / 10
        }
        return 0
}

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if footerText != nil {
            let cell = UITableViewCell()
            cell.frame = CGRect(origin: CGPoint(x: 0, y: self.tableView.frame.height - self.tableView.rowHeight), size: CGSize(width: self.tableView.frame.width, height: self.tableView.frame.height / CGFloat(10)))
            cell.backgroundColor = UIColor.red

            let textLabel = UILabel(frame: CGRect(origin: CGPoint.zero, size: cell.frame.size))
            textLabel.text = footerText

            cell.addSubview(textLabel)

            return cell
        }
        return nil
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.tableView.frame.height / 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell()

        // Clean

        for subview in cell.subviews {
            subview.removeFromSuperview()
        }

        // Configure

        if indexPath.row == sourceData.count - 1 && footerInline {
            cell.backgroundColor = UIColor.red
        }
        else
        {
            cell.backgroundColor = UIColor.blue
        }

        let textLabel = UILabel(frame: CGRect(origin: CGPoint.zero, size: cell.frame.size))
        textLabel.text = sourceData[indexPath.row]

        cell.addSubview(textLabel)

        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

要测试这一点,请将 viewDidLoad 中 for... 循环的范围从 0..<5 更改为 0..<50。您会看到该行为与您想要实现的目标相匹配。

希望有帮助!