iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource'

后端 未结 4 2167
广开言路
广开言路 2020-12-29 05:43
  • I am new to IOS swift development. I used to work with previous Xcode 6 beta.

  • I have downloaded the Xco

相关标签:
4条回答
  • 2020-12-29 06:31

    Try this simple solution:

    import UIKit
    
    class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
    {
        //MARK: - Life Cycle
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        //MARK: - Tableview Delegate & Datasource
        func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int
        {
            return 10
        }
    
        func numberOfSectionsInTableView(tableView: UITableView) -> Int
        {
            return 1
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
    
            let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
            return cell
        }
    
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
        {
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-29 06:33

    In my case, i named my UITableView as tableView. That caused this same error:

    'MyViewController' does not conform to protocol 'UITableViewDataSource'

    including another error:

    Candidate is not a function

    And changed tableview name to something else, error is gone.

    0 讨论(0)
  • 2020-12-29 06:36
    class ROTableView: UITableView, UITableViewDelegate {
    
        func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
            return 10
        }
    
        func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
            let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
    
    //        cell.text = "Row #\(indexPath.row)"
    //        cell.detailTextLabel.text = "Subtitle #\(indexPath.row)"
    
            return cell
        }
    
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
        }
    }
    

    When you create the subclass for UITableView please refer the above code, you will not get any error.

    0 讨论(0)
  • 2020-12-29 06:44

    You need to look at the whole error message. This specific message includes additional information about which methods are missing:

    Type 'MyViewController' does not conform to protocol 'UITableViewDataSource'
    Protocol requires function 'tableView(_:numberOfRowsInSection:)' with type '(UITableView, numberOfRowsInSection: Int) -> Int'
    Candidate has non-matching type '(UITableView!, numberOfRowsInSection: Int) -> Int'
    

    So... your numberOfRowsInSection takes an optional UITableView, and should take a required UITableView (this is a change they made between 6 and 6.1, all UITableView delegate and datasource methods now take required tableView and indexPath values)

    0 讨论(0)
提交回复
热议问题