Swift 3 UITableView数据源方法viewForHeaderInSection发出警告

Nik*_*mov 19 uitableview swift swift3 ios10 xcode8

迁移到Swift 3后,我有以下方法:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {}
Run Code Online (Sandbox Code Playgroud)

它给了我警告

实例方法'tableView(tableView:viewForHeaderInSection :)'几乎匹配协议'UITableViewDataSource'的可选要求'tableView(_:titleForHeaderInSection :)'

Fix-it提供将方法设为私有或添加@"nonobjc"注释.如何解决警告?

Pav*_*kal 27

我的应用程序上都有类似的警告.实际上有2个问题.我通过在方法签名中添加下划线或将方法移动到实现方法来源的协议的右扩展来修复所有警告.

我认为你的问题可能是两者的结合.

更详细:

1)您可能忘记在"tableView:..."之前添加"下划线"字符,这使得它在Swift 3中成为一种不同的方法(在Swift 2.3中无关紧要).所以你应该改变这个:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Run Code Online (Sandbox Code Playgroud)

对此:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Run Code Online (Sandbox Code Playgroud)

2)该方法tableView(_:viewForHeaderInSection:)来自UITableViewDelegate协议,但看起来编译器不知道这个方法 - 它只知道方法,UITableViewDataSource并试图建议你其中一个(tableView(_:titleForHeaderInSection:)).所以你要么根本没有实现UITableViewDelegate,要么你实现,但在另一个扩展中可能?

  • 实际上问题的原因是该方法被添加到数据源扩展而不是委托.谢谢你指出这一点. (9认同)
  • 对我来说也一样,只是添加了`UITableViewDelegate` (2认同)