2017-04-12 49 views
2

我有一個連接到某些項的TabBar視圖。[UIViewController中的tableView:numberOfRowsInSection:]:無法識別的選擇發送到實例

我想,這些項目中的一個包含TableView,但我不希望使用TableViewController,因爲我希望把別的東西在頁面的頭部。

ViewController實現2個協議UITableViewDataSourceUITableViewDelegate幷包含以下功能:

 func tableView(_ tableView: UITableView, numberOfRowsInSection 
     section: Int) -> 
    Int { 
     // Return the number of rows in the section. 
     return annunci.count 
    } 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
     IndexPath) -> 
    UITableViewCell { 
     let cellIdentifier = "Cell" 
     let cell = tableView.dequeueReusableCell(withIdentifier: 
     cellIdentifier, for: indexPath) 
     // Configure the cell... 
     cell.textLabel?.text = annunci[indexPath.row] 
     return cell } 

然後,從故事板我添加具有標識符「小區」的原型細胞和鏈接的tableViewviewController作爲數據源和代表。

當我運行程序的時候是好的,但是當我選擇包含表查看應用程序的項目將引發此異常:

 *** Terminating app due to uncaught exception 
    'NSInvalidArgumentException', reason: '-[UIViewController 
     tableView:numberOfRowsInSection: 
     unrecognized selector sent to instance 0x101c29370' 

我怎樣才能把東西在頁面的頂部,然後在的tableView或者我怎樣才能解決這個問題?

我使用的斯威夫特3的Xcode 8.2.1

+0

您是否在包含tableView(也是tableView的delegate和dataSource)的類中實現了這些方法? – jokeman

+0

是的,我只有那個班級 –

回答

1

如果你沒有設置視圖控制器類的故事板可能發生這種情況。

在你的故事板,選擇具有UITableView視圖控制器並轉到身份檢查(在右窗格中第三個選項卡)。確保視圖控制器的類設置爲自定義類,而不是UIViewController

identity inspector

1

對我來說,是我忘了包括委託和數據源。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ... } 

其他原因,在一個新的新項目see this minimal working version of a UITableView.運行它,你的代碼與它比較,以發現的bug。

+0

我在做同樣的錯誤。謝謝 :) –

相關問題