繁体   English   中英

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

[英]Type 'ViewController' does not conform to protocol '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 之外编写了它们。 此外,您需要更改行数的功能。

建议的编辑

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

尝试删除! 在你的功能上。 那对我有用

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

我来到这篇文章并开始把事情放在一起,同时牢记我对 Objective-C 编程的微薄知识。 错误的原因是表格视图由两个元素定义,首先是部分和部分中的行,其次是表格视图单元格。 默认情况下,表视图中至少有一个节,但我们需要关于节中行数的一致性。 其次,我们需要知道我们将在一个部分的特定行中呈现哪个单元格。 无论如何,即使我们使用默认的 UITableViewCell,我们仍然需要一个标识符来访问它以设置它的子视图或属性。 我希望它有帮助,因为我自己对 Swift 很陌生,所以我会感谢一些软批评:)

以下代码在 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.
    }
}

我刚刚在 Swift 中遇到了同样的问题。

你应该实现类中 UITableViewDataSource 的所有功能,也就是说应该实现以下功能:

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

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

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

我不确定缺少 numberOfSectionsInTableView 的功能是否适合您。 对我来说,我必须在我的课堂上意识到这一点。

只需添加这两个方法即可解决该错误[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 的所有选项

只是一个提高可读性的建议,您可以使用扩展来分离您的协议,如下所示:

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 ,在列表中查找所需的方法并让自动完成完成其工作。

自动完成

这是一个常见的警告,表示“您尚未实现协议所需的方法”

故事板上的视图对象可能需要数据源。 例如,TableView 需要一个数据源,通常,视图控制器充当一个。

所以表视图期望 ViewController 包含返回表视图的“必备”信息的方法。

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

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

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

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

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

这在使用 swift 3 编译器编译时对我有用

Ankit的答案在 Xcode 8 for Swift 2.3 中对我有用。 这是新语法。

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

}

通过自动完成建议的一些编辑,来自@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。

在官方文档中,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM