James
James

Reputation: 31

Type "First View Controller" does not conform to protocol "UITableViewDataSource"

So I can't seem to get this error away..

My code is:

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 used both of the required functions for UITableViewDataSource so I'm not sure why I keep getting this error.

Upvotes: 3

Views: 3124

Answers (3)

Thomás Pereira
Thomás Pereira

Reputation: 9778

You need to implement 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 de line:

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

Hope It helps!

Upvotes: 0

Wayne
Wayne

Reputation: 60414

Neither of each method's respective tableView parameters should be optionals

Upvotes: 1

Nitin Arora
Nitin Arora

Reputation: 2668

Try changing the method signatures as mentioned below. Notice ! is removed in the input params.

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

}

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

Upvotes: 3

Related Questions