ios - UITableView 无法识别 NumberOfRowsInSection

标签 ios swift sqlite uitableview sections

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

-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x101c3ce50 2017-10-14 17:14:44.893258+0200 MinistryHelp[403:44322] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x101c3ce50' * First throw call stack: (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: terminating with uncaught exception of type 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
}

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

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

最佳答案

不要猜测方法的正确签名。有两种方法可以做到这一点。

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

这是正确的方法签名:

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

您缺少下划线。

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

func numberOfSections(in tableView: UITableView) -> Int

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

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

关于ios - UITableView 无法识别 NumberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746074/

相关文章:

android - 有没有办法在安装新版本时删除旧版本应用程序?

iOS - 开发构建工作但分发构建崩溃

ios - 可以从非主线程可靠地调用 UIApplication.shared.applicationState 吗?

ios - Swift - UITableViewController 和 Storyboard 未链接

ios - AWS DynamoDB 在 LIST 或 NUMBER SET 中插入值

android - 获取到一个点的最短距离

ios - 取消firebase功能

ios - 使用 dequeueReusableCellWithIdentifier :forIndexPath:? 时的新单元格或现有单元格

ios - 将事件传递给 iOS Objective C 中的另一个对象

sql - 从表中获取值以在插入中使用它