2017-10-14 41 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中)並使用屬性來保存結果數組。那麼你的數據源和委託方法應該簡單地訪問該屬性。