2017-10-14 75 views
0

我收到此错误。 我正在使用SQLite。我有一些数据,我想在部分显示数据,所以我添加了一个列,这将是我的部分名称,称为“cittaTerritorio”。 现在我取回我的数据,当我运行与此消息的代码一切都崩溃使用此列, 排序是:UITableView无法识别NumberOfRowsInSection

- [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 0x18a7 8fc44 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 
} 

与第一功能我得到基于列表我的方法返回的指数部分的名称。 第二个函数根据该部分返回数据的数量,所以我根据该部分的名称过滤数据,并使用列表返回数字。 第三种和第四种方法返回部分的数量。

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

+0

'numberOfRowsInSection'的签名是错误的。注释掉方法并使用代码完成重新输入它,或者查看[UITableViewDataSource](https://developer.apple.com/documentation/uikit/uitableviewdatasource)文档中的方法。 – vadian

+0

PS:并且**从不**在'numberOfRows'和'numberOfSections'中执行数据库操作。这些方法经常被调用。创建一个数据源数组并获取一次数据。 – vadian

+0

我看到问题出在某种程度上,我有2个numberOfRowsInSection方法 – John

回答

-1

您是否设置了所有代表和数据源?如果没有,试试这个在viewDidLoad中()方法:

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

如果这不适合你,让我知道,我会尽量多想想。

+0

是的,我已经做到了 – John

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变化很大。

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