ios 8 swift - 如何使用单独的数据源为表视图添加页脚
ios 8 swift - how to add footer for tableview using separate datasource
这两天我一直在努力解决这个问题。
我只想在我的表格视图中添加一个页脚(自定义单元格)。
我有一个视图,上面有一些东西(标签、按钮),并且我添加了一个表格视图。
为了有一个干净的控制器,我为数据源使用了一个单独的文件:
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.
如前所述,显示页眉和页脚需要实现UITableViewDelegate
协议的相关方法。对于页脚,它是:
tableView(tableView: UITableView, viewForFooterInSection section: Int)
确保您的 table 视图的委托设置正确。
另外,请注意还有另一个方便的 table 视图功能:将添加到整个 table 视图下方的页脚,无论数据源或委托实现如何。只需分配一个 tableFooterView
。
Swift 3(注意下划线):
override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
这两天我一直在努力解决这个问题。 我只想在我的表格视图中添加一个页脚(自定义单元格)。 我有一个视图,上面有一些东西(标签、按钮),并且我添加了一个表格视图。 为了有一个干净的控制器,我为数据源使用了一个单独的文件:
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.
如前所述,显示页眉和页脚需要实现UITableViewDelegate
协议的相关方法。对于页脚,它是:
tableView(tableView: UITableView, viewForFooterInSection section: Int)
确保您的 table 视图的委托设置正确。
另外,请注意还有另一个方便的 table 视图功能:将添加到整个 table 视图下方的页脚,无论数据源或委托实现如何。只需分配一个 tableFooterView
。
Swift 3(注意下划线):
override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat