【问题标题】:tableView:numberOfRowsInSection: unrecognized selector sent to instance errortableView:numberOfRowsInSection:无法识别的选择器发送到实例错误
【发布时间】:2020-10-07 19:26:06
【问题描述】:

我遇到了这个烦人的问题。我已经搜索了很多。很多人建议了很多事情。 就像添加了这些 TableViewDataSourceTableViewDelegate

class SalesNewViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, UITableViewDataSource

连接参考插座;

并通过代码连接数据源和委托;

TableView.dataSource = self
TableView.delegate = self

在没有 performSegue 的情况下尝试 performSegue 时出现此错误(例如,如果我想通过单击 TableViewCellhello 打印到控制台>) 它没有给出任何错误;

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "SalesSegue", sender: self)
}

我在另一个视图中使用相同的东西,它可以正常工作。任何帮助都会非常可爱。

编辑 1: 我有两个必需的方法正在运行。 numberOfRowsInSectioncellForRowAt

【问题讨论】:

    标签: swift uitableview swift5


    【解决方案1】:

    如果您在 Xcode 帮助系统中搜索“UITableViewDataSource”,您将获得有关该协议的文章。它说:

    Only two methods of this protocol are required, and they are shown in the following example code.
    
    // Return the number of rows for the table.     
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 0
    }
    
    // Provide a cell object for each row.
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       // Fetch a cell of the appropriate type.
       let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)
       
       // Configure the cell’s contents.
       cell.textLabel!.text = "Cell text"
           
       return cell
    }
    

    你谈论连接的东西,但你没有提到实现这些方法。

    假设您的视图控制器是表格视图的数据源,您的视图控制器至少需要实现这两种方法(numberOfRows(inSection:)cellForRow(at:))。

    您可以将它们放在视图控制器的主体中,也可以放在为UITableViewDataSource 协议提供协议一致性的扩展中。

    (请注意,Apple 提供的 numberOfRows(inSection:) 示例代码将导致您的表格视图为空。您需要将 return 0 位替换为返回 your 中的行数的代码strong> 表格视图,无论在您的应用中意味着什么。)

    【讨论】:

    • 我很遗憾没有提到我有这些方法。实际上;我有他们。没有 performSegue 功能,没有问题,但是当我尝试这样做时,它给了我错误。
    • 只有在点击单元格触发 performSegue 时才会出现错误?然后,segue 调用的视图控制器似乎是正在下降的视图控制器。在由 segue "SalesSegue" 调用的视图控制器中的 init 方法、viewDidLoad 和表视图数据源和委托方法中添加断点
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多