简体   繁体   English

UITableView底部的静态单元格

[英]Static cell at the bottom of a UITableView

I have a table view filled with contacts. 我有一个充满联系人的表格视图。 At the very bottom of the table view (after the last contact) I would like to have a static table view cell that is always there with custom content. 在表格视图的最底部(最后一次联系之后),我希望有一个始终带有自定义内容的静态表格视图单元格。

How can I accomplish this? 我该怎么做?

Lets say that the array you are using to populate your UITableView is called 'contactsArray'. 假设您用来填充UITableView的数组称为“ contactsArray”。

What you would need to do in the following delegate method is add an additional row eg: 在以下委托方法中,您需要做的是添加另外一行,例如:

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

    return contactsArray+1

}

Then you would need to handle this logic eg: 然后,您将需要处理此逻辑,例如:

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

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

Here's the example: 这是示例:

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
}

And associated function: 以及相关功能:

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

Here's the result: 结果如下:

在此处输入图片说明

Another way! 其他方式! We can implement by using UITableViewDelegate . 我们可以使用UITableViewDelegate来实现。 And here's the example below, how we done. 这是下面的示例,我们如何做。

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

Note: Second option is always visible on the main screen like a section header. 注意:第二个选项始终在主屏幕上显示,就像节标题一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM