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

前端之家收集整理的这篇文章主要介绍了Swift 3 UITableView数据源方法viewForHeaderInSection给出警告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
迁移到Swift 3后,我有以下方法
func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {}

它给了我警告

Instance method ‘tableView(tableView:viewForHeaderInSection:)’ nearly
matches optional requirement ‘tableView(_:titleForHeaderInSection:)’
of protocol ‘UITableViewDataSource’

Fix-it提供使方法私有或添加@“nonobjc”注释。如何解决警告?

我的应用程序中有类似的警告。实际上有两个问题。我通过将下划线添加方法签名中或通过将方法移动到实现方法来自的协议的正确扩展来修复所有警告。

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

更详细地说:

1)您可能忘记在“tableView:…”之前添加“下划线”字符,这使得它在Swift 3中是不同的方法(在Swift 2.3中并不重要)。所以你应该改变这个:

func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView?

到这个:

func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView?

2)方法tableView(_:viewForHeaderInSection :)来自UITableViewDelegate协议,但是看起来编译器不知道这个方法 – 它只知道来自UITableViewDataSource的方法,并尝试建议你们之一(tableView(_ :titleForHeaderInSection :))。所以你根本不执行UITableViewDelegate,或者你也可以执行另一个扩展?

原文链接:https://www.f2er.com/swift/320338.html

猜你在找的Swift相关文章