簡體   English   中英

UITableView底部的靜態單元格

[英]Static cell at the bottom of a UITableView

我有一個充滿聯系人的表格視圖。 在表格視圖的最底部(最后一次聯系之后),我希望有一個始終帶有自定義內容的靜態表格視圖單元格。

我該怎么做?

假設您用來填充UITableView的數組稱為“ contactsArray”。

在以下委托方法中,您需要做的是添加另外一行,例如:

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

    return contactsArray+1

}

然后,您將需要處理此邏輯,例如:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if indexPath.row >= contactsArray.count{
        print("Last Row")
    }
} 

這是示例:

override func viewDidLoad() {
    super.viewDidLoad()

    let button = UIButton.init()
    button.frame = CGRect.init(x: 0, y: 0, width: 100, height: 44)
    button.setTitle("✚", for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
    button.setTitleColor(UIColor.darkGray, for: .normal)
    button.addTarget(self, action: #selector(handle(sender:)), for: .touchUpInside)

    self.tableView.tableFooterView = button
}

以及相關功能:

func handle(sender: UIButton) {
    print("Tapped")
}

結果如下:

在此處輸入圖片說明

其他方式! 我們可以使用UITableViewDelegate來實現。 這是下面的示例,我們如何做。

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let button = UIButton.init()
        button.frame = CGRect.init(x: 0, y: 0, width: 100, height: 44)
        button.setTitle("✚", for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.addTarget(self, action: #selector(handle(sender:)), for: .touchUpInside)

        return button
    }

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


    func handle(sender: UIButton) {
        print("Tapped")
    }

注意:第二個選項始終在主屏幕上顯示,就像節標題一樣。

暫無
暫無

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

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