【问题标题】:'NSInvalidArgumentException' - [UIViewController tableView:numberOfRowsInSection:] - unrecognized selector sent to instance'NSInvalidArgumentException' - [UIViewController tableView:numberOfRowsInSection:] - 发送到实例的无法识别的选择器
【发布时间】:2023-03-07 15:31:01
【问题描述】:

我知道这是一个重复的问题(非常抱歉),但是到目前为止我已经尝试了我在 SO 上找到的所有内容,但我仍然收到错误这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc4a9705960'

让我用图像清楚地阐明零件,以便我们可以为解决相关问题提供一个很好的答案。

这是文件夹视图(ViewController - 主视图,TableView1 是子视图):

我的TableView1.xib 文件有:

我还编辑了我的TableView1.swift

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
}

在我的Main.Storyboard

当我选择 View Controller () 时,它看起来像这样:

但是,当我点击View)时:

我在路上做错了什么或错过了什么?

更多信息:

【问题讨论】:

  • 谁是TableView1.xib的所有者,必须是TableView1.swift
  • TableView1.swift 是什么类?可能 .xib 中的文件应该设置为该类。
  • @vadian 没错。 TableView1.swift.xib 应该是 ViewController.swiftMain.storyboard 的孩子
  • 选择 tableview1.xib 并在左上角找到所有者
  • @Alok,是 tableview.xib > 文件的所有者 > 吗? .它的自定义类 > 类是 TableView1 和 Module > Current..

标签: ios iphone swift uitableview


【解决方案1】:
-[UIViewController tableView:numberOfRowsInSection:]:

因此,根据错误日志,当您设置 deletegate 和 datasource 时,您的类型不匹配。这是因为在 xib 上您说 tableview1.xib 应该实现数据源和委托。但是在 ViewController1.swift 中,您错误地引用了 UIViewController。 现在你看到 UIViewController 对实现委托和数据源是未知的。因为它没有发现(显然)它崩溃了。 所以:

let controller : UIViewController = UIViewController(nibName: "TableView1", bundle: nil)

改为:

let controller : UITableViewController = UITableViewController(nibName: "TableView1", bundle: nil)

应该可以。

【讨论】:

    【解决方案2】:

    你是否在其他地方调用了这个函数?

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    

    }

    看起来这个函数在委托之外被调用了。

    【讨论】:

    • 他没有明确地称呼它
    • 没错。我只是在TableView1.swift 中调用它(因为它是由 New Swift Cacao Class 自动创建的
    【解决方案3】:

    不知道 Swift,但是 iOS 上的 ObjC...

    异常表示类 UIViewController 无法处理消息 [UIViewController tableView:numberOfRowsInSection:]。这表明,在某些时候,dataSource 更改为“super”。

    您需要“覆盖”吗?您没有覆盖消息实现,因为 UIViewController 没有实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 2013-06-12
      • 2013-12-26
      • 2012-07-16
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多