使用故事板
在 UITableView 中你可以拖动 UIView,如果你有超过 0 个原型单元格,它将设置为 FooterView。拖动后,您可以在表格视图层次结构中将其视为子视图。现在,您可以在该视图上添加标签按钮,也可以将 IBAction 设置为 ViewController 类文件。
以编程方式
遵循 3 个步骤
- 使用按钮制作一个自定义视图,
Swift 3.X / Swift 4.X
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
customView.addSubview(button)
Swift 2.X
let customView = UIView(frame: CGRectMake(0, 0, 200, 50))
customView.backgroundColor = UIColor.redColor()
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", forState: .Normal)
button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
customView.addSubview(button)
- 在表尾视图中添加该视图。
Swift 2.X/Swift 3.X/Swift 4.X
myTblView.tableFooterView = customView
- 您可以在同一类中对该按钮执行操作。
Swift 3.X/Swift 4.X
@objc func buttonAction(_ sender: UIButton!) {
print("Button tapped")
}
Swift 2.X
func buttonAction(sender: UIButton!) {
print("Button tapped")
}