ios 8 swift – 如何使用单独的数据源为tableview添加页脚

2天,我试图解决这个问题。 我只想添加一个页脚(自定义单元格)到我的tableview。 我有一些东西(标签,button)的视图,我已经添加了一个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的一部分,而不是UITableViewController的一部分。

所以,我的问题是,如何实现?

谢谢。

CC

如上所述,显示页眉和页脚需要实现UITableViewDelegate协议的相关方法。 对于页脚是:

 tableView(tableView: UITableView, viewForFooterInSection section: Int) 

确保您的表视图的委托设置正确。

另外请注意,还有另一个方便的表视图function:无论数据源或委托实现如何,都会在整个表视图下添加一个页脚。 只分配一个tableFooterView

Swift 3(注意下划线):

 override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat