pmac89
pmac89

Reputation: 418

UITableView and UITableViewDataSource

So, I'm getting a 'Type ViewController does not conform to protocol UITableViewDataSource' error in Xcode. I've implemented the required functions:

I've even made sure I've told Xcode how many sections there are (which is not required). This is a Single View Application and I have ensured my references are setup correctly in Storyboard. So my Outlets are my view and my table view. My Referencing Outlets are set for dataSource and delegate to be my Table View.

Here's the code.

ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    var items: [String] = ["We", "Dislike", "Swift", "Very", "Much", "Right", "Now"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

        tableView.delegate = self
        tableView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        return 1
    }

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

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

        let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as UITableViewCell
        cell.textLabel?.text = self.items[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)!")
    }

}

EDIT: Also, I know that I shouldn't need the tableView.delegate and tableView.dataSource as I have assigned them in the Storyboard but I thought I'd include them to make sure you all know that I know to have them set.

Upvotes: 1

Views: 2000

Answers (2)

Kirsteins
Kirsteins

Reputation: 27345

UITableViewDataSource protocol has changed a little bit. Try:

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

Upvotes: 2

Nate Cook
Nate Cook

Reputation: 93286

Your tableView(tableView:cellForRowAtIndexPath) signature is wrong - it should be:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
    let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as UITableViewCell
    cell.textLabel?.text = self.items[indexPath.row]

    return cell
}

Note that it doesn't return an optional value (i.e., no ! suffix) any more.

Upvotes: 1

Related Questions