类型视图控制器不符合协议UITableViewDataSource

时间:2014-10-07 02:48:59

标签: uitableview swift

我已经阅读了关于这个主题的其他问题,代码看起来是O.K.对我来说? 关于错误是什么的任何想法?

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
        }


    }
}

1 个答案:

答案 0 :(得分:1)

你覆盖错误的签名。用下面的签名替换你的功能。不需要implicit optional并且还要添加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
    }


}