-[UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例

-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

我是 Swift 的新手,因为我几个月前才开始使用它。这个错误让我很头疼。

-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance XXX

这是我目前已经收集并实施的可能修复的列表。

  1. 我已将自定义 Class 更改为 CheckboxQuestion class 而不是 UIViewController class。

Custom Class

  1. 我已将表视图连接到数据源和委托。

  2. 我已经解释了所有松散的参考渠道。

  3. 我已经为每个原型单元命名了可重复使用的标识符出口。

prototype cell 1

尽管如此,我仍然收到上述错误。

异常错误的断点发生在appdelegateclass这一行

class AppDelegate: UIResponder, UIApplicationDelegate {

还有什么我想念的吗?

这是 CheckboxQuestion 的代码 Class 代码

import UIKit

var checkboxCellInfo = ["Field 1", "Field 2"]

class CheckboxQuestion: UIViewController {

@IBOutlet weak var tableView: UITableView!

//1. determine number of rows of cells to show data
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
    return checkboxCellInfo.count + 1
}

//2. inputs info into each cell from array 'cellInfo'
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell? {

    if indexPath.row <= checkboxCellInfo.count - 1 {

        let cell:CheckboxQuestionCellConnect = self.tableView.dequeueReusableCellWithIdentifier("CheckboxNumber") as! CheckboxQuestionCellConnect

        return cell 

     } else {

        let cell:CheckboxQuestionCellConnect = self.tableView.dequeueReusableCellWithIdentifier("CheckboxAdd") as! CheckboxQuestionCellConnect

        return cell

    }  
}

//3. determines height of each cell
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 60
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

您正在实施 UITableViewDelegate/UITableViewDataSource 方法,但您的 class 似乎不符合其中任何一个。由于您继承自 UIViewController,因此您需要添加这些协议并将 table 视图的 delegatedataSource 设置为 self

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

如果视图控制器只显示一个视图控制器,您应该考虑使用 UITableViewController 而不是 UIViewController,那么上面的内容不适用 - 您可以通过 UITableViewController。但是我无法从代码中判断是否是这种情况。

我认为在这种情况下您可能忘记了将表视图委托和数据源分配给自己。

self.tableView.dataSource = 自己

self.tableView.delegate = 自己

请确保您从代码中删除了 outlet,而不是从 storyboard.just 右键单击​​故事板中的 tableview。删除 outlet 并重新连接 it.May 因为您已经为同一个 tableview 添加了两个 outlet。

确保您已在下方添加。

class ViewController: UIViewController 
      ,UITableViewDataSource,UITableViewDelegate{

并且还将 delegatedatasource 连接到 stoaryboard 中的 tableview

有时您会在不同的 viewcontroller 中添加相同的 class 名称。其中之一 viewcontroller 有一个表格视图。

您可能错过了 UITableView 协议实现,确保您实现了需要实现的协议

  1. UITableViewDelegate
  2. UITableViewDataSource

在 h 文件上添加协议实现:

@interface YourClass : UIViewController <UITableViewDelegate,UITableViewDataSource>

@end

在 m 文件上添加所需的方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

有时您没有将文件与情节提要连接起来。

就我而言,我提供了: 1.所有网点都对, 2.数据源和委托权,

我缺少的是: 我没有在情节提要中为 UIViewController 提供 class 和情节提要标识符。因此显示

-[UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例

因为一个普通的UIViewController没有这些方法,当我们符合UITableViewDatasource时,我们在我们自定义的UIViewController中提供它class。

所以请在故事板中检查 UIViewController 的身份检查器,并在那里提供正确的 class。

考虑下图:

确保你的控制器中有这个。

extension NameController: UITableViewDelegate { // code... }

extension NameController: UITableViewDataSource { // code... }

就我而言,我没有将故事板中的 ViewController 连接到我的自定义 class