加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:51:04 所属栏目:百科 来源:网络整理
导读:迁移到Swift 3后,我有以下方法: func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) - UIView? {} 它给了我警告 Instance method ‘tableView(tableView:viewForHeaderInSection:)’ nearly matches optional requirement ‘t
迁移到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,或者你也可以执行另一个扩展?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读