2017-04-12 118 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

我在做同样的错误。谢谢 :) –

相关问题