ios Swift 在 UITableView 中添加页脚视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38178509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift Add Footer View In UITableView
提问by mimi93
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?
这实际上是一个tableview和tableview单元格,我想在tableview单元格的末尾添加一个提交按钮,但我该怎么做?
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.
我想做下面的截图。
回答by Ujesh
Using StoryBoard
使用故事板
In UITableView You can drag UIView, it will set as FooterView if you have more then 0 prototype cell. After Drag you can see it in table view hierarchy as a subview. Now, you can add the label button on that View, you can also set IBAction into ViewController Class File.
在 UITableView 中,您可以拖动 UIView,如果您有 0 个以上的原型单元格,它将设置为 FooterView。拖动后,您可以在表视图层次结构中看到它作为子视图。现在,您可以在该视图上添加标签按钮,您还可以将 IBAction 设置为 ViewController 类文件。
Programmatically
以编程方式
Follow 3 Steps
遵循 3 个步骤
- Make one custom view with button,
- 使用按钮制作一个自定义视图,
Swift 3.X/ Swift 4.X
斯威夫特 3.X/斯威夫特 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
斯威夫特 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)
- Add that view in Table Footer View.
- 在表页脚视图中添加该视图。
Swift 2.X/Swift 3.X/Swift 4.X
斯威夫特 2.X/斯威夫特 3.X/斯威夫特 4.X
myTblView.tableFooterView = customView
- you can do action on that button in same class.
- 您可以在同一类中对该按钮执行操作。
Swift 3.X/Swift 4.X
斯威夫特 3.X/斯威夫特 4.X
@objc func buttonAction(_ sender: UIButton!) {
print("Button tapped")
}
Swift 2.X
斯威夫特 2.X
func buttonAction(sender: UIButton!) {
print("Button tapped")
}
回答by K.Gowri Manohari
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
return footerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50
}
回答by Abhijith
Swift 3/4
斯威夫特 3/4
1. If you want to add Table footer which is only visible at the end of TabelView
1.如果要添加仅在TabelView末尾可见的Table页脚
let customView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 150))
customView.backgroundColor = UIColor.clear
let titleLabel = UILabel(frame: CGRect(x:10,y: 5 ,width:customView.frame.width,height:150))
titleLabel.numberOfLines = 0;
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = PTConstants.colors.darkGray
titleLabel.font = UIFont(name: "Montserrat-Regular", size: 12)
titleLabel.text = "Payment will be charged to your Apple ID account at the confirmation of purchase. Subscription automatically renews unless it is canceled at least 24 hours before the end of the current period. Your account will be charged for renewal within 24 hours prior to the end of the current period. You can manage and cancel your subscriptions by going to your account settings on the App Store after purchase."
customView.addSubview(titleLabel)
tableView.tableFooterView = customView
2. If you want to add section footer which is visible while scrolling through section.
2. 如果要添加在滚动部分时可见的部分页脚。
Adopt UITableViewDelegate and implement following delegate method.
采用 UITableViewDelegate 并实现以下委托方法。
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let vw = UIView()
vw.backgroundColor = UIColor.clear
let titleLabel = UILabel(frame: CGRect(x:10,y: 5 ,width:350,height:150))
titleLabel.numberOfLines = 0;
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.backgroundColor = UIColor.clear
titleLabel.font = UIFont(name: "Montserrat-Regular", size: 12)
titleLabel.text = "Footer text here"
vw.addSubview(titleLabel)
return vw
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 150
}
回答by Bary Levy
Add UILabel as UITableView.Footer in swift
快速添加 UILabel 作为 UITableView.Footer
let footerView:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width:320 , height: 500))
footerView.text = "add description ro Timevc in order user will see the result of lesson time"
footerView.numberOfLines = 0;
footerView.sizeToFit()
tableView.tableFooterView = footerView
tableView.contentInset = (UIEdgeInsetsMake(0, 8, -footerView.frame.size.height, 8))