簡體   English   中英

將一個單元格放在uitableview的底部

[英]Place a cell at bottom of uitableview

如何在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 | |
                                                      | +------------+ |
                                                      +----------------+

午餐時我已經快速瀏覽了一下。 我最初雖然這可以通過重寫ScrollViewDidScroll來完成,但無法通過這種方式實現。 我想出的答案是動態決定表格是否應顯示頁腳視圖。

在下面的代碼中(這是粗略的,但應該給你一個想法),我們測試表的行數是否大於我們想要在屏幕上的數量(在這種情況下,我將其硬編碼為10)。 如果是,那么我們從源數據數組的最后一個元素創建一個頁腳,並將頁腳高度設置為與其他單元格相同的高度,具有不同的背景顏色。 如果沒有,那么我們使用cellForItemAtIndexPath來測試我們是否在dataSource的最后一行,在這種情況下我們相應地格式化單元格:

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
    }
}

要對此進行測試,請將viewDidLoad中for ...循環的范圍從0 .. <5更改為0 .. <50。 您會看到行為符合您想要達到的目標。

希望有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM