craig Shell
craig Shell

Reputation: 1

Type view controller does not conform to protocol UITableViewDataSource

I have read the other questions on this subject, the code look O.K. to me? any ideas as to what the error is?

import UIKit

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource
{



    @IBOutlet weak var myTableView: UITableView!

    var arrayOfPersons: [Person] = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.setUpPersons()

      //  self.myTableView.delegate = self
      //  self.myTableView.dataSource = self

    }

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

    func setUpPersons()
    {
        var person1 = Person(name: "Bill", number: 60, imageName: "bill-gates.jpg")

        var person2 = Person(name: "Sara", number: 39, imageName: "sara-evans.jpg")

        arrayOfPersons.append(person1)
        arrayOfPersons.append(person2)


    }

    func tableView(tableView:UITableView!, numberOfRowsInSection section: Int)->Int
    {
        return arrayOfPersons.count

    }

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

        let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell")as CustomCell



        if indexPath.row % 2 == 0
        {
        cell.backgroundColor = UIColor.purpleColor()
        }
        else{
            cell.backgroundColor=UIColor.orangeColor()

        }

        let person = arrayOfPersons[indexPath.row]


        return cell
        }


    }
}

Upvotes: 0

Views: 1345

Answers (1)

codester
codester

Reputation: 37189

You are overiding wrong signature.Replace your function with below ones.No need for implicit optional and also add override

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{
    return arrayOfPersons.count

}



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

    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell")as CustomCell



    if indexPath.row % 2 == 0
    {
    cell.backgroundColor = UIColor.purpleColor()
    }
    else{
        cell.backgroundColor=UIColor.orangeColor()

    }

    let person = arrayOfPersons[indexPath.row]


    return cell
    }


}

Upvotes: 1

Related Questions