Swift 3 UITableView 数据源方法 viewForHeaderInSection 给出警告
Swift 3 UITableView datasource method viewForHeaderInSection gives warning
迁移到Swift3后我有以下方法:
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:..." 之前添加 "underscore" 字符,这使得它在 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
要么你实现了,但也许在另一个扩展中?
迁移到Swift3后我有以下方法:
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:..." 之前添加 "underscore" 字符,这使得它在 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
要么你实现了,但也许在另一个扩展中?