我试图解决这个问题2天。 我只想在我的tableview中添加一个页脚(自定义单元格)。 我有一些视图(标签,按钮),我添加了一个tableview。 为了拥有一个干净的控制器,对于数据源,我使用的是一个单独的文件:
class MyDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
...
}
在我的控制器中我有:
class MyViewController: UIViewController {
@IBOutlet weak var myButton: UIButton!
var myDatasource: MyDataSource!
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myDatasource = MyDataSource(....)
myTableView.dataSource = myDatasource
// Do any additional setup after loading the view.
}
所以,在MyDataSource
我添加了:
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.blackColor()
return footerView
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0
}
这段代码没有被调用。
我查看了文档,我发现这两个最后的方法是UITableViewController
的一部分而不是UITableViewDataSource
的一部分。
所以,我的问题是,如何实现这一目标?
感谢。
C.C。
答案 0 :(得分:4)
Swift 3(注意下划线):
override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
答案 1 :(得分:2)
如上所述,显示页眉和页脚需要实现UITableViewDelegate
协议的相关方法。对于页脚,它是:
tableView(tableView: UITableView, viewForFooterInSection section: Int)
确保正确设置了表格视图的委托。
另外,请注意,还有另一个方便的表视图功能:将在整个表视图下方添加一个页脚,而不管数据源或委托实现如何。只需指定tableFooterView
即可。