【问题标题】:UITableView unrecognized NumberOfRowsInSectionUITableView 无法识别 NumberOfRowsInSection
【发布时间】:2018-03-26 13:03:05
【问题描述】:

我收到此错误。 我正在使用 SQLite。我有一些数据,我想在部分中显示数据,所以我添加了一个列作为我的部分名称,称为“cittaTerritorio”。 现在我正在检索我的数据并使用此列对其进行排序, 当我运行代码时,一切都会因这条消息而崩溃:

-[UIView tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 0x101c3ce50 2017-10-14 17:14:44.893258+0200 MinistryHelp[403:44322] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x101c3ce50 ' * 首先抛出调用栈: (0x1812b3d38 0x1807c8528 0x1812c11f8 0x18aa7bcc4 0x1812b96e4 0x18119f0dc 0x18aa02cc8 0x18a7908d8 0x18a7900a4 0x18a78fe34 0x18a78fc44 0x18a81c14c 0x1008cfc60 0x1008cf040 0x1008cfedc 0x18a6c3bfc 0x18a76c128 0x18a76b5c8 0x18a76afcc 0x18a76aa34 0x18a76a95c 0x18a6c1000 0x1852910b4 0x185295194 0x185203f24 0x18522a340 0x18522b180 0x18125b8b8 0x181259270 0x18125982c 0x18117a2d8 0x18300bf84 0x18a727880 0x1008c62d0 0x180c9e56c) libc++abi.dylib:以 NSException 类型的未捕获异常终止

你有什么想法吗?那是我的代码:

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.listCitta()[section]
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.territoriInCitta(section: section).count
}

func numberOfSections(in tableView: UITableView) -> Int {
    return self.listCitta().count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.listCitta().count
}

func territoriInCitta(section: Int)-> [String]{
    let nomeSezione = tableview.headerView(forSection: section)?.textLabel?.text
    var listaCitta = [String]()
    listaCitta.removeAll()
    do{
        let territori = try self.database.prepare(territoryTable.filter(cittaTerritorio == nomeSezione!))
        for territorio in territori{
            listaCitta.append(territorio[self.cittaTerritorio])
        }
    }catch{
        print(error)
    }
    return listaCitta
}

func listCitta()-> [String]{ //aggiunge i vari nomi delle citta
    var listaCitta = [String]()
    listaCitta.removeAll()
    do{
        let terr = try self.database.prepare(territoryTable.select(cittaTerritorio))
        for territorio in terr{
            if listaCitta.contains(territorio[self.cittaTerritorio]){
                //nil
            }
            else{
                listaCitta.append(territorio[self.cittaTerritorio])
            }
        }
    }catch{
        print(error)
    }
    return listaCitta
}

使用第一个函数,我根据我的方法返回的列表的索引获取部分名称。 第二个函数根据部分返回数据的数量,因此我根据该部分名称过滤数据,然后使用列表返回数字。 第三和第四个方法返回节数。

我也试过插入假数据,写一个数字用于测试,没有什么不同。

【问题讨论】:

  • numberOfRowsInSection 的签名错误。注释掉该方法并使用代码完成重新键入它或在UITableViewDataSource 文档中查找该方法。
  • PS:并且从不numberOfRowsnumberOfSections中进行数据库操作。这些方法经常被调用。创建一个数据源数组并获取一次数据。
  • 我发现问题在于我有 2 个 numberOfRowsInSection 方法

标签: ios swift sqlite uitableview sections


【解决方案1】:

不要猜测该方法的正确签名。有两种方法可以让它正确。

  1. 最好的选择是让 Xcode 给你正确的签名。开始输入名称(例如“numberOfRows”),Xcode 将显示可能的匹配方法。选择正确的。
  2. 另一种选择是从参考文档中复制正确的签名。

这是正确的方法签名:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

您缺少下划线。

为什么你要两次尝试numberOfSections 方法?只需使用正确的:

func numberOfSections(in tableView: UITableView) -> Int

您很可能从一些 Swift 2 代码中复制并粘贴了示例。 Swift 3 中的 API 发生了很大变化。

与您的问题无关(如 cmets 中所述),请勿从您的任何表视图数据源或委托方法调用 listCitta()。您应该调用一次(例如在viewDidLoad 中)并使用属性来保存结果数组。然后,您的数据源和委托方法应该只访问该属性。

【讨论】:

    【解决方案2】:

    您是否设置了所有的委托和数据源?如果没有,请在 viewDidLoad() 方法中尝试:

    override func viewDidLoad() {
         super.viewDidLoad()
         myTableView.delegate = self
         myTableView.dataSource = self
    }
    

    如果这对你不起作用,请告诉我,我会尝试更多。

    【讨论】:

      【解决方案3】:

      确保在您各自的视图控制器中实现了 UITableViewDelegateUITableViewDataSource

      class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-04
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多