Joe
Joe

Reputation: 87

UITableViewDataSource error in Swift2

I'm learning Swift and, coding some lines, an error appear on the screen showing: Type JVViewController (class name) does not conform to protocol 'UITableViewDataSource'. I don't know why this appear in my file if I did exactly the same in other apps and I never did this kind of problem.

Please, let me know how to solve this situation. Thanks!

Upvotes: 2

Views: 252

Answers (1)

dede.exe
dede.exe

Reputation: 1310

If a class does not conform with a protocol, it means you need to implement some protocol methods. In case on the UITableViewDataSource, is required to implement the follow methods:

It defines the quantity of tableViewCell you will show on your tableView

tableView(_:numberOfRowsInSection:)

It defines the settings of each tableViewCell

tableView(_:cellForRowAtIndexPath:)

Maybe you forgot to implement one or this two dataSource methods.

E.g.:

class JVViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    var collectionItems = ["One", "Two", "Three"]    

    ...

    //Quantity of rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.collectionItems.count;
    }

    //Row settings
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = self.collectionItems[indexPath.row]

        return cell
    }

    ...

}

You can find out more in Apple documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/

Upvotes: 2

Related Questions