【发布时间】: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:并且从不在
numberOfRows和numberOfSections中进行数据库操作。这些方法经常被调用。创建一个数据源数组并获取一次数据。 -
我发现问题在于我有 2 个 numberOfRowsInSection 方法
标签: ios swift sqlite uitableview sections