Impulso
Impulso

Reputation: 27

Receiving error "Type ViewController does not conform to protocol 'UITableViewDataSource..." even though I have the required functions

Please see my code below. I am receiving error "Type ViewController does not conform to protocol 'UITableViewDataSource'". I have researched this error and other answers say that UITableViewDataSource requires certain functions, but I have included those functions. What else could be wrong? I am using Xcode 7.3.

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var zipCodeText: UITextField!
    @IBOutlet weak var zipTable: UITableView!

    var zipArray: [String] = []


    override func viewDidLoad() {
        super.viewDidLoad()
        self.zipTable.delegate = self
        self.zipTable.dataSource = self
    }

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

    @IBAction func addZipButton(sender: UIButton) {
        zipArray.append(zipCodeText.text!)
        zipTable.beginUpdates()
        zipTable.insertRowsAtIndexPaths([
            NSIndexPath(forRow: zipArray.count-1, inSection: 0)
            ], withRowAnimation: .Automatic)
        zipTable.endUpdates()
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")!
        cell.textLabel?.text = zipCodeText.text
    }

}

Upvotes: 0

Views: 1203

Answers (1)

InnisBrendan
InnisBrendan

Reputation: 2249

You are not returning your cell in tableView(_:cellForRowAtIndexPath:).

Try something like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")!
    cell.textLabel?.text = zipCodeText.text
    // Make sure to include the return statement
    return cell
}

In the docs you will find information about the return value: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/#//apple_ref/occ/intfm/UITableViewDataSource/tableView:cellForRowAtIndexPath:

Upvotes: 3

Related Questions