繁体   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