Javier Conreras
Javier Conreras

Reputation: 45

type 'ThirdViewController' does not conform to protocol UITableViewDataSource

I have started my first app, and I want the app to have a view that works as a to-do list, however, I'm getting type 'ThirdViewController' does not conform to protocol UITableViewDataSource as an error in my code. I already looked at a similar thread but found no solution there

import UIKit

class ThirdViewController: UIViewController, UITableViewDataSource, UITableViewDelegate  
{
    var list = ["Math Homework", "English Project", "Bio Quiz"]

    @IBOutlet weak var myTableView: UITableView!

    override func viewDidLoad() 
    {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() 
    {
        super.didReceiveMemoryWarning()
    }


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

    public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
        cell.textLabel?.text = list[indexPath.row]

        return cell
    }
}

Upvotes: 0

Views: 379

Answers (2)

VitorMM
VitorMM

Reputation: 1078

As specified in the Apple Developer documentation, you need the following methods:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

You instead used these:

public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

Remove public from both methods and it should work. Also, add _ before tableView:.

Reference:

https://developer.apple.com/reference/uikit/uitableviewdatasource

EDIT: The methods that I described are for Swift 3. In case you are using Swift 2.3, these should be your methods:

func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

Almost the same that you used in the first place, but without public.

Upvotes: 2

Shoaib
Shoaib

Reputation: 2294

Implement the required protocol methods for UITableViewDataSource and UITableViewDelegate.

you are missing;

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

Upvotes: 0

Related Questions