简体   繁体   English

类型“ViewController”不符合协议“UITableViewDataSource”

[英]Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

Started practice swift.开始快速练习。 In singleViewController I am trying to make a UITableView .在 singleViewController 我试图制作一个UITableView In storyboard I set the datasource and delegate.在故事板中,我设置了数据源和委托。 Here I am getting the error * 'ViewController' does not conform to protocol 'UITableViewDataSource' *在这里我收到错误*'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

}

You should implement all the required methods before the last } , but you have written them outside of the UIViewController.您应该在最后一个}之前实现所有必需的方法,但是您已经在 UIViewController 之外编写了它们。 Also, you need to change the func for number of lines.此外,您需要更改行数的功能。

the suggested edit建议的编辑

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
    }
}

Try removing the !尝试删除! on your func.在你的功能上。 That did the job for me那对我有用

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
}

You need to implement all the required methods of UITableViewDataSource in order to get rid of that error.您需要实现UITableViewDataSource所有必需方法才能消除该错误。

Basically... you're missing:基本上......你错过了:

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

I had the same problem, things would workout rather easily in Objective-C, probably because we are more familiar with it at the moment, but in this case, swift is very new, therefore, the its error notifications are rather vague.我遇到了同样的问题,在 Objective-C 中事情会很容易解决,可能是因为我们现在更熟悉它,但在这种情况下,swift 是非常新的,因此,它的错误通知相当模糊。

While I was implementing the UITableView based application, and I ran into this problem.在我实现基于 UITableView 的应用程序时,我遇到了这个问题。 I opened up the implementation file for UITableView by pressing command and clicking on UITableView.我通过按下命令并单击 UITableView 打开了 UITableView 的实现文件。 In implementation file, we can clearly see that two functions are mandatory to implement,在实现文件中,我们可以清楚地看到两个函数是强制实现的,

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

I arrived at this post and started to put things together while keeping my meager knowledge of objective-C programming in mind.我来到这篇文章并开始把事情放在一起,同时牢记我对 Objective-C 编程的微薄知识。 Reason for the error being that a table view is defined by two elements, first the section and rows in a section, and second the tableview cells.错误的原因是表格视图由两个元素定义,首先是部分和部分中的行,其次是表格视图单元格。 By default there is at least one section in the table view, but we need conformance about number of rows in a section.默认情况下,表视图中至少有一个节,但我们需要关于节中行数的一致性。 Secondly, we need to know which cell are we going to present in a particular row in a section.其次,我们需要知道我们将在一个部分的特定行中呈现哪个单元格。 Regardless,even if we are using default UITableViewCell, we still need an identifier to access it in order to set its subviews or properties.无论如何,即使我们使用默认的 UITableViewCell,我们仍然需要一个标识符来访问它以设置它的子视图或属性。 I hope it was helpful, A bit soft criticism will be appreciated since I am myself very new to Swift :)我希望它有帮助,因为我自己对 Swift 很陌生,所以我会感谢一些软批评:)

The following code didn't work for me on iOS 8.1.以下代码在 iOS 8.1 上对我不起作用。 in XCode 6.1.1.在 XCode 6.1.1 中。 This code works:此代码有效:

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.
    }
}

I had just faced the same problem in Swift.我刚刚在 Swift 中遇到了同样的问题。

You should realize all the functions for the UITableViewDataSource in the class, which means the following functions should be realized:你应该实现类中 UITableViewDataSource 的所有功能,也就是说应该实现以下功能:

func numberOfSectionsInTableView(tableView: UITableView) -> Int{}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}

I'm not sure whether missing the function for numberOfSectionsInTableView works for you or not.我不确定缺少 numberOfSectionsInTableView 的功能是否适合您。 For me, I have to realize it in my class.对我来说,我必须在我的课堂上意识到这一点。

Just Add these two methods then that error will be resolved[XCode8 Swift3]只需添加这两个方法即可解决该错误[XCode8 Swift3]

first method :第一种方法:

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

Second Method :第二种方法:

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

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

Just a suggestion to improve readability, you can separate your protocols using extension, like this:只是一个提高可读性的建议,您可以使用扩展来分离您的协议,如下所示:

class ViewController: UIViewController {
    // Your lifecycle code here
}

extension ViewController: UITableDataSource { 
    func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
         return 20
    }
}

extension ViewController: UITableViewDelegate {
    ...
}

This is probably caused by a typo or wrong styling on the method name.这可能是由方法名称上的拼写错误或样式错误引起的。 I used to cmd + left click on UITableView and copy & paste method names on my UIViewController ;我曾经使用 cmd + 左键单击UITableView并在我的UIViewController上复制和粘贴方法名称; which won't work on Swift .这不适用于Swift

Instead, type func tableView , look for the desired method on the list and let auto complete do its job.相反,键入func tableView ,在列表中查找所需的方法并让自动完成完成其工作。

自动完成

This is a common warning which means that " you haven't implemented the required methods of the protocol yet"这是一个常见的警告,表示“您尚未实现协议所需的方法”

A view object on the storyboard may need a datasource.故事板上的视图对象可能需要数据源。 For example, TableView needs a datasource and usually, View Controller acts as one.例如,TableView 需要一个数据源,通常,视图控制器充当一个。

So Table View expects the ViewController to contain the methods which return the 'must have' information for the table view.所以表视图期望 ViewController 包含返回表视图的“必备”信息的方法。

Table view needs to know the number of sections, number of rows in each section etc..表视图需要知道节的数量,每个节的行数等。

Unless all the required information is returned by the datasource object, the warning will persist.除非数据源对象返回所有必需的信息,否则警告将持续存在。

Change the syntax of "UITableViewDataSource" protocol required methods as per new swift 3 documentation:根据新的 swift 3 文档更改“UITableViewDataSource”协议所需方法的语法:

internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int内部 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int

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

This has worked for me while compiling with swift 3 compiler这在使用 swift 3 编译器编译时对我有用

The answer of Ankit worked for me in Xcode 8 for Swift 2.3. Ankit的答案在 Xcode 8 for Swift 2.3 中对我有用。 Here's the new syntax.这是新语法。

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
    }
}

Add these methods添加这些方法

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

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

Copy the code below under the ViewController class and specify the number of rows you want (in the 1st section) and define the content of each cell (in 2nd function)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.
}

} }

with some edits suggested by auto complete, from the above answer by @MB_iOSDeveloper on swift 3, Xcode 8.3.2, this code works for me:通过自动完成建议的一些编辑,来自@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.
}

} }

In general, a protocol has optional and required methods.通常,协议具有可选和必需的方法。 For example, UISearchBarDelegate and UITableViewDelegate are the cases that you can declare to conform to a protocol without implementing any of their methods.例如, UISearchBarDelegateUITableViewDelegate是您可以声明符合协议而不实现任何方法的情况。 But it does not work fine for UITableViewDataSource.但它不适用于UITableViewDataSource。

In the official documentation, Protocol "UITableViewDataSource" -> Symbols -> Configuring a Table View, the methods: func tableView(UITableView, cellForRowAt: IndexPath) and func tableView(UITableView, numberOfRowsInSection: Int) shown with bold Required key word.在官方文档中,Protocol "UITableViewDataSource" -> Symbols -> Configure a Table View,方法: func tableView(UITableView, cellForRowAt: IndexPath)func tableView(UITableView, numberOfRowsInSection: Int)用粗体的Required关键字表示。

只需删除 viewDidLoad() 和 Build 并添加 viewDidLoad() 一切正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型的viewcontroller不符合协议uitableviewdatasource - type viewcontroller does not conform to protocol uitableviewdatasource 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type “ViewController” does not conform to protocol 'UITableViewDataSource" 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type'ViewController' does not conform to protocol 'UITableViewDataSource' 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type 'ViewController' does not conform to protocol 'UITableViewDataSource' ViewController不符合协议UITableViewDataSource - ViewController does not conform to protocol UITableViewDataSource Xcode类型“ ViewController”中出现错误不符合协议“ UITableViewDataSource” - Getting error in Xcode type “ViewController” does not conform to protocol“UITableViewDataSource” 与“类型&#39;ViewController&#39;不符合协议&#39;UITableViewDataSource&#39;”相关的编译和构建错误 - Compile & Build error related to “Type 'ViewController' does not conform to protocol 'UITableViewDataSource'” 错误:类型“ UserAccView”不符合协议“ UITableViewDataSource” - error : type “UserAccView” does not conform to protocol 'UITableViewDataSource' 类型TypeViewOne不符合协议UITableViewDataSource - type TypeViewOne does not conform to protocol UITableViewDataSource 收到错误“ Type ViewController不符合协议&#39;UITableViewDataSource…”,即使我具有必需的功能 - Receiving error “Type ViewController does not conform to protocol 'UITableViewDataSource…” even though I have the required functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM