UITableViewDelegate方法不会在Swift 3.0和Xcode 8迁移之后调用

在将我的代码库从Swift 2.2迁移到Swift 3.0之后,我注意到我的UITableView页脚没有显示出来。 事实certificate,我的UITableViewDelegate方法没有调用(例如: func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? )。

有趣的是, UITableViewDataSource方法正在被调用, 表被填充 。 我已经设置父视图控制器是表的delegatedataSource

为了说明这个问题,我创build了一个示例Swift 3.0项目 ,以尽可能匹配我现有的代码库。 也许Swift 3 / Xcode 8中的一些东西我没有意识到,或者我可能会错过一些非常明显的东西。 感谢您的帮助!

在我检查了你的示例项目之后:

你没有做错什么,但是指向viewForFooterInSection ,它不会被调用,直到你实现heightForFooterInSection方法:

 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 50 // for example } 

类似案例:

没有为heightForHeaderInSection ==实现>即使实现,也不需要调用viewForHeaderInSection

numberOfRowsInSection返回为0 ==>即使实现了cellForRowAt也不会调用它。

附加说明:您不必在viewDidLoad()添加tableView.dataSource = selftableView.delegate = self ,它们已经在界面生成器中设置。

希望有所帮助。

你应该设置页脚的高度

  func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 15 } 

在Xcode8或最新的Swift版本中,方法签名已被更改。 此更改可能会导致您的代码不调用委托方法。

通过使用自动完成再次写入方法来修复它。

旧方法 –

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat? { return 50.0 } 

新方法 –

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50.0 } 

在使用viewForHeaderInSection时 ,必须使用heightForHeaderInSection

希望这有助于。