Matt Spoon
Matt Spoon

Reputation: 2830

Getting error in Xcode type "ViewController" does not conform to protocol"UITableViewDataSource"

I'm trying to follow a table views tutorial but I'm getting an error on the line of my class declaration which says: "type "ViewController" does not conform to protocol"UITableViewDataSource"" here is my code:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    private let dwarves = ["Sleepy", "Sneezy", "Bashful", "Happy", "Doc", "Grumpy", "Dopey", "Thorin", "Dorin", "Nori", "Ori", "Balin", "Dwalin", "Fili", "Kili", "Oin", "Gloin", "Bifur", "Bofur", "Bombur"]

    let simpleTableIdentifier = "SimpleTableIdentifier"

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as? UITableViewCell
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: simpleTableIdentifier)
        }

        cell!.textLabel!.text = dwarves[indexPath.row]
        return cell!
    }


}

Upvotes: 0

Views: 96

Answers (3)

The Tom
The Tom

Reputation: 2810

UITableViewDataSource has two required methods:

tableView:cellForRowAtIndexPath:

and

tableView:numberOfRowsInSection:

Both need to be implemented.

You are missing the second one.

You can find more info on UITableViewDataSource here : HERE

Upvotes: 0

Paul van Roosendaal
Paul van Roosendaal

Reputation: 1434

You're missing a required method for the datasource protocol: tableView:numberOfRowsInSection:

See: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/

Upvotes: 0

JZAU
JZAU

Reputation: 3567

You missed the func tableView(tableView: UITableView, numberOfRowsInSection secion: Int) -> Int {}

This is necessary for a tableview.

So just add this to your code:

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

Upvotes: 3

Related Questions