types'ViewController'不符合协议'UITableViewDataSource'

开始练习迅速。 在singleViewController我试图做一个UITableView 。 在故事板中,我设置了数据源和委托。 这里我得到错误*'ViewController'不符合协议'UITableViewDataSource'*

截图错误

 class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var table: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } func numberOfSectionsInTableView(tableView: UITableView!) -> Int { return 20 } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell") cell.textLabel.text="row#\(indexPath.row)" cell.detailTextLabel.text="subtitle#\(indexPath.row)" return cell } 

你应该在最后一个}之前实现所有必需的方法,但是你已经把它们写在了UIViewController之外。 此外,你需要改变行数的func。

build议编辑

 class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var table: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { return 20 } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell") cell.textLabel.text="row#\(indexPath.row)" cell.detailTextLabel.text="subtitle#\(indexPath.row)" return cell } } 

尝试删除! 在你的function。 那为我做了这份工作

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell") cell.textLabel.text="row#\(indexPath.row)" cell.detailTextLabel.text="subtitle#\(indexPath.row)" return cell } 

你需要实现UITableViewDataSource所有必需的方法,以摆脱这个错误。

基本上…你想念:

 func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { //return XX } 

我遇到了同样的问题,在Objective-C中很容易出现问题,可能是因为我们目前比较熟悉,但是在这种情况下,swift是非常新的,所以它的错误通知是非常模糊的。

当我正在实现基于UITableView的应用程序时,我遇到了这个问题。 我通过按命令并单击UITableView打开了UITableView的实现文件。 在实现文件中,我们可以清楚地看到两个函数是强制执行的,

  1. func tableView(tableView:UITableView,numberOfRowsInSection部分:Int) – > Int
  2. func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) – > UITableViewCell

我来到这个post,开始把所有东西放在一起,同时保持我对Objective-C编程的微薄的认识。 错误的原因是表视图是由两个元素定义的,首先是一个节中的节和行,其次是tableview单元格。 默认情况下,表格视图中至less有一个部分,但我们需要符合一个部分中的行数。 其次,我们需要知道我们要在一个部分的某一行中呈现哪个单元格。 无论如何,即使我们使用默认的UITableViewCell,我们仍然需要一个标识符来访问它,以设置其子视图或属性。 我希望这是有帮助的,有点温和的批评将不胜感激,因为我自己很新,迅速:)

我刚刚在Swift面临同样的问题。

您应该在类中实现UITableViewDataSource的所有function,这意味着应该实现以下function:

 func numberOfSectionsInTableView(tableView: UITableView) -> Int{} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} 

我不知道是否缺lessnumberOfSectionsInTableView函数适用于您。 对我来说,我必须在我的课堂上认识到这一点。

以下代码在iOS 8.1上不适用于我。 在XCode 6.1.1中。 此代码工作:

 import UIKit class ViewController : UIViewController,UITableViewDelegate,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ //currently only a testing number return 25 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell") cell.textLabel?.text = "row#\(indexPath.row)" cell.detailTextLabel?.text = "subtitle#\(indexPath.row)" return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

只需添加这两个方法,那么错误将被解决[XCode8 Swift3]

第一种方法:

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } 

第二种方法:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } 

取出最新的Xcode 6.1.1 GM_Seed的tableview和NSIndexpath的所有选项

只是一个提高可读性的build议,你可以使用扩展来分隔你的协议,就像这样:

 class ViewController: UIViewController { // Your lifecycle code here } extension ViewController: UITableDataSource { func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { return 20 } } extension ViewController: UITableViewDelegate { ... } 

这可能是由方法名称上的错字或错误样式造成的。 我曾经cmd +左键单击UITableView并复制粘贴方法名称在我的UIViewController ; 这在Swift不起作用。

相反,键入func tableView ,在列表中查找所需的方法,并让auto自动完成工作。

自动完成

这是一个常见的警告,这意味着“你还没有实现协议的必要的方法”

故事板上的视图对象可能需要数据源。 例如,TableView需要一个数据源,通常,View Controller就像一个一样。

所以Table View期望ViewController包含返回表视图'必须'信息的方法。

表视图需要知道段的数量,每段的行数等。

除非数据源对象返回所有必需的信息,否则警告将持续。

根据新的swift 3文档更改“UITableViewDataSource”协议所需的方法的语法:

内部func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int) – > Int

内部func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) – > UITableViewCell

这在我用swift 3编译器编译的时候很有效

Ankit在Swift 2.3的Xcode 8中为我工作。 这是新的语法。

 extension ViewController: UITableViewDataSource { internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //return 1 } internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //return cell } } 

添加这些方法

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } 

复制ViewController类下面的代码,并指定你想要的行数(在第一部分),并定义每个单元格的内容(在第二个函数中)

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 //no. of rows in the section } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: <#T##UITableViewCellStyle#>, reuseIdentifier: <#T##String?#>) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 

}

用自动完成的一些编辑build议,从上面的答案通过@MB_iOSDeveloper在swift 3,Xcode 8.3.2上,这段代码适用于我:

 class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { override func viewDidLoad() { super.viewDidLoad() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ //currently only a testing number return 25 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "mycell") cell.textLabel?.text = "row#\(indexPath.row)" cell.detailTextLabel?.text = "subtitle#\(indexPath.row)" return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 

}

通常,协议具有可选的和所需的方法。 例如, UISearchBarDelegateUITableViewDelegate是您可以声明以符合协议而无需实现任何方法的情况。 但是它不适用于UITableViewDataSource。

在官方文档中,协议“UITableViewDataSource” – > Symbols – >configuration一个Table View,方法: func tableView(UITableView, cellForRowAt: IndexPath)func tableView(UITableView, numberOfRowsInSection: Int)