ios - Swift 在 UITableView 中添加页脚 View

标签 ios iphone swift uitableview storyboard

这实际上是一个表格 View 和表格 View 单元格,我想在表格 View 单元格结束后添加一个提交按钮,但我该怎么做呢?

我尝试在 Storyboard 中手动添加按钮,但它不起作用,按钮没有显示。还有其他方法吗?

我想像下面的截图一样。

最佳答案

使用 Storyboard

在 UITableView 中,你可以拖动 UIView,如果你有超过 0 个原型(prototype)单元格,它将设置为 FooterView。拖动后,您可以在 TableView 层次结构中看到它作为 subview 。现在,您可以在该 View 上添加标签按钮,您还可以将 IBAction 设置到 ViewController 类文件中。

以编程方式

遵循 3 个步骤

  1. 使用按钮制作一个自定义 View ,

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)
  1. 在表页脚 View 中添加该 View 。

Swift 2.X/Swift 3.X/Swift 4.X

myTblView.tableFooterView = customView
  1. 您可以在同一个类中对该按钮执行操作。

swift 3.X/ swift 4.X

@objc func buttonAction(_ sender: UIButton!) {
    print("Button tapped")
}

swift 2.X

func buttonAction(sender: UIButton!) {
  print("Button tapped")
}

关于ios - Swift 在 UITableView 中添加页脚 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178509/

相关文章:

ios - 完全黑色的 UITableVIew Controller

iphone - UIView backgroundColor与图像

iPhone 和 WireShark

iphone - 选择多张图片

通过枚举快速递增

swift - 如何屏蔽 Swift 中的最后一个数字?

ios - 如何使用自定义 GKComponent 创建 SKSpriteNode?

ios - Cocos2d 2.0 横向 : howto?

ios - 强制标签文本从左到右,同时包含一些从右到左的文本

ios - performSegueWithIdentifier 根本不工作