2014-10-07 98 views
0

我已閱讀關於此主題的其他問題,代碼看O.K.對我來說? 關於錯誤是什麼的任何想法?類型視圖控制器不符合協議UITableViewDataSource

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 
     } 


    } 
} 
+0

嘗試刪除 「UITableViewDataSource」。 – 2014-10-07 03:34:59

+0

在DataSource函數中刪除(!)。 – Ramesh 2014-10-07 04:02:28

回答

1

您overiding錯signature.Replace你下面ones.No需要功能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 
    } 


} 
相關問題