The "First View Controller" type does not conform to the "UITableViewDataSource" protocol

So I can't get this error.

My code:

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tblTasks: UITableView!

    //UITableViewDataSource
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
        return taskMgr.tasks.count
    }

    //UITableViewDataSource
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")

        cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
        cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].desc

        return cell
    }
}

      

I've used both of the required functions for UITableViewDataSource

, so I'm not sure why I keep getting this error.

+3


source to share


3 answers


Try changing the method signatures like below. Notice ! removed in the input parameters.



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

}

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

      

+3


source


None of the methods matching the parameters tableView

should be



+1


source


You need to implement the two required methods: tableView: numberOfRowsInSection and tableView: cellForRowAtIndexPath .

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayDataSource.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "cell"
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

    // information here

    return cell
}

      

If you have a custom cell, change only the line:

var cell:MyCustomCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MyCustomCell

      

Hope this helps!

0


source







All Articles