示例如下:
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")
}
注意:第二个选项在主屏幕上始终可见,就像部分标题一样。