【问题标题】:Table View cellForRowAtIndexPath warning表视图 cellForRowAtIndexPath 警告
【发布时间】:2016-06-22 13:52:04
【问题描述】:

我已将代码更新为 swift 3.0,并在以下行收到警告:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

当我尝试使用每个建议以使用 @nonobjc 使警告静音或将其设为私有函数时,表不再加载。

错误显示:

实例方法 'tableView(:cellForRowAtIndexPath:)' 几乎匹配协议 'UITableViewDelegate' 的可选要求 'tableView(:canFocusRowAt:)'

有谁知道导致此错误的原因以及如何解决?

非常感谢!

【问题讨论】:

    标签: swift uitableview swift3


    【解决方案1】:

    只需将实现 UITableViewDataSource 协议的声明添加到类定义中,如下所示:

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {}
    

    【讨论】:

      【解决方案2】:

      在 swift 3.0 中,数据源的签名更改为:

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      

      注意cellForRowAtIndexPath indexPath: IndexPathcellForRowAt indexPath: IndexPath 之间的区别

      我正在使用新方法,没有任何警告,希望这能解决您的问题。

      干杯。

      【讨论】:

      • 我还没有检查 swift3,但真的吗?这种变化没有任何意义..背后有什么原因吗?还是只是为了删除“indexPath”的多余使用?
      • remove the redundant 确实是 Swift 3 中的一个重要主题。
      • 我已经使用了该代码并进行了清理,但它仍然给我同样的错误:(当我展开错误时,它给出了几个指标:Move 'tableView(_:cellForRowAt:)' to an extension to silence this warning & Requirement 'tableView(_:canFocusRowAt:)' declared here (UIKit.UITableViewDelegate)
      • 我认为是因为您没有在视图控制器中实现 UITableViewDataSource
      • @MAB 你可能是对的。我的 UITableViewDataSource 是视图控制器外部的模型对象,我遇到了同样的错误。为了将其减少为警告,我必须在函数声明 @objc(tableView:didSelectRowAtIndexPath:) internal func tableView.... 中添加以下前缀。
      【解决方案3】:

      我也遇到过类似的问题,发现如果去掉tableView前下划线之间的空格,从此

      func tableView(_ tableView: ...
      

      到这里

      func tableView(_tableView: ...
      

      奇怪的是警告消失了......

      【讨论】:

        猜你喜欢
        • 2011-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        • 2020-05-25
        相关资源
        最近更新 更多