【问题标题】:Type 'ViewController' does not conform to protocol 'UITableViewDataSource'类型“ViewController”不符合协议“UITableViewDataSource”
【发布时间】:2014-08-30 11:19:22
【问题描述】:

开始快速练习。在 singleViewController 中,我试图创建一个 UITableView。在情节提要中,我设置了数据源和委托。在这里我收到错误 * 'ViewController' does not conform to protocol '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

}

【问题讨论】:

  • 您的函数 numberOfSectionsInTableViewcellForRowAtIndexPath 需要在类中。将它们向上移动,就在didReceiveMemoryWarning 的定义下方。
  • 错误消息明确指出要实现 UITableViewDataSource 的“必需”委托,即 numberOfRowsInSection 和 cellForRowAtIndexPath 方法。
  • 编译器抛出该错误的第一个原因是它正在寻找它在 UITableViewDatasource 中找到的所需方法,而您缺少 numberOfRowsInSection 方法。
  • 您如何真正知道需要实施哪些委托?我控制点击 UITableViewDataSource 并列出了几个方法

标签: ios uitableview swift


【解决方案1】:

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

建议的编辑

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

【讨论】:

  • 您在 GM Xcode 6 上试过吗?我正在使用 func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int 但我被告知“候选人的类型不匹配”
  • 在 XCode 6.1 中不起作用。尝试删除!完全来自 tableView 方法。
【解决方案2】:

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

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
}

【讨论】:

  • 也为我工作。谢谢!
  • 请解释为什么删除'!'允许类符合
【解决方案3】:

您需要实现UITableViewDataSource 的所有必需方法才能消除该错误。

基本上......你错过了:

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

【讨论】:

  • 我认为,这首先是错误的原因。
【解决方案4】:

我也遇到了同样的问题,在 Objective-C 中事情会比较容易,可能是因为我们现在更熟悉它,但是在这种情况下,swift 是非常新的,因此它的错误通知相当模糊.

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

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

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

【讨论】:

    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

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

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

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

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

      【讨论】:

        【解决方案7】:

        只需添加这两个方法即可解决该错误[XCode8 Swift3]

        第一种方法:

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

        第二种方法:

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

        【讨论】:

          【解决方案8】:

          为最新的 Xcode 6.1.1 GM_Seed 去掉 tableview 和 NSIndexpath 的所有选项

          【讨论】:

            【解决方案9】:

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

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

            【讨论】:

              【解决方案10】:

              这可能是由于方法名称的拼写错误或样式错误造成的。我以前用 cmd + 左键单击 UITableView 并在我的 UIViewController 上复制和粘贴方法名称;这不适用于Swift

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

              【讨论】:

                【解决方案11】:

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

                情节提要上的视图对象可能需要数据源。例如,TableView 需要一个数据源,通常 View Controller 充当一个。

                所以 Table View 期望 ViewController 包含返回 table view 的“必须拥有”信息的方法。

                表格视图需要知道节数、每个节的行数等。

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

                【讨论】:

                  【解决方案12】:

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

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

                  internal func tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

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

                  【讨论】:

                    【解决方案13】:

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

                    【讨论】:

                      【解决方案14】:

                      添加这些方法

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

                      【讨论】:

                        【解决方案15】:

                        复制下面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.
                        }
                        

                        }

                        【讨论】:

                          【解决方案16】:

                          根据@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.
                          }
                          

                          }

                          【讨论】:

                            【解决方案17】:

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

                            在官方文档中,Protocol "UITableViewDataSource" -> Symbols -> 配置表格视图,方法: func tableView(UITableView, cellForRowAt: IndexPath)func tableView(UITableView, numberOfRowsInSection: Int) 以粗体 必填 关键字显示。

                            【讨论】:

                              【解决方案18】:

                              只需删除 viewDidLoad() 并构建并添加 viewDidLoad() 一切正常

                              【讨论】:

                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多