Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

后端 未结 17 2480
旧巷少年郎
旧巷少年郎 2020-11-29 03:02

Started practice swift. In singleViewController I am trying to make a UITableView. In storyboard I set the datasource and delegate. Here I am getting the error

相关标签:
17条回答
  • 2020-11-29 03:25

    Take out all the optionals for tableview and NSIndexpath for the latest Xcode 6.1.1 GM_Seed

    0 讨论(0)
  • 2020-11-29 03:25

    Change the syntax of "UITableViewDataSource" protocol required methods as per new swift 3 documentation:

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

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

    This has worked for me while compiling with swift 3 compiler

    0 讨论(0)
  • 2020-11-29 03:30

    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. But it does not work fine for 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.

    0 讨论(0)
  • 2020-11-29 03:32

    I had just faced the same problem in Swift.

    You should realize all the functions for the UITableViewDataSource in the class, which means the following functions should be realized:

    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. For me, I have to realize it in my class.

    0 讨论(0)
  • 2020-11-29 03:32

    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)

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

    }

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