本文介绍了Swift在UITableView中添加页脚视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is actually a tableview and tableview cell, and i wanted to add a Submit button after the end of the tableview cell, but how do i do it?

我试图在故事板上手动添加按钮,但它不起作用,按钮没有显示。还有其他办法吗?

I tried to do it at the storyboard add the button manually, but its not working, the button is not showing. Is there any other way to do it?

我想做的就像下面的截图。

I wanted to do like the screenshot below.

推荐答案

使用StoryBoard

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

In TableView You can drag UIView, it will set as FooterView if you have more then 0 prototype cell. After Drag you can see it in tableview hierarchy as subview.Now, you can add label button on that View, you can also set IBAction into ViewController Class File.

以编程方式

按照3个步骤


  1. 使用按钮创建一个自定义视图,

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. 在表格页脚视图中添加该视图。

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

这篇关于Swift在UITableView中添加页脚视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 08:03
查看更多