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

后端 未结 17 2478
旧巷少年郎
旧巷少年郎 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:10

    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.

    While I was implementing the UITableView based application, and I ran into this problem. I opened up the implementation file for UITableView by pressing command and clicking on UITableView. In implementation file, we can clearly see that two functions are mandatory to implement,

    1. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    2. 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. 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. I hope it was helpful, A bit soft criticism will be appreciated since I am myself very new to Swift :)

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

    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; which won't work on Swift.

    Instead, type func tableView, look for the desired method on the list and let auto complete do its job.

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

    Just Add these two methods then that error will be resolved[XCode8 Swift3]

    first method :

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

    Second Method :

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    }
    
    0 讨论(0)
  • 2020-11-29 03:13

    Add these methods

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    }
    
    0 讨论(0)
  • 2020-11-29 03:16

    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
    }
    
    0 讨论(0)
  • 2020-11-29 03:16

    The following code didn't work for me on iOS 8.1. in 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.
        }
    }
    
    0 讨论(0)
提交回复
热议问题