2016-01-14 49 views
2

我得到的編碼時的錯誤,說類型「視圖控制器」不符合協議「UITableViewDataSource」

類型‘視圖控制器’不符合協議‘UITableViewDataSource’

任何人都可以告訴了什麼錯這個

import UIKit 

class ViewController: UIViewController, UITableViewDataSource{ 

    let devCourses = [ 
     ("Math"), 
     ("Science"), 
     ("English"), 
     ("Computer Programming"), 
     ("Physics")] 
    func numberOfSectionsInTableview(tableview: UITableView)-> Int { 
     return 1 
    } 
    func tableview(tableView: UITableView, numberOfRowsInSection section: Int) ->Int{ 
     return devCourses.count 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = UITableViewCell() 

     let (courseTitle) = devCourses[indexPath.row] 

     cell.textLabel?.text = courseTitle 
     return cell 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

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

您是否嘗試將父級更改爲UITableViewController? –

+0

[類型「ViewController」可能重複不符合協議「UITableViewDataSource」](https://stackoverflow.com/questions/28241279/type-viewcontroller-does-not-conform-to-protocol-uitableviewdatasource) – SmokeDispenser

+0

我把「tableview」而不是「tableView」,這就是爲什麼我犯了錯誤。感謝球員的幫助! –

回答

3

我認爲你缺少的是由委託所需的一些功能:?

聲明應改爲包括:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

,然後你要確保所有的表所需的方法有:

func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int 

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

和在ViewDidLoad上調用代理

override func viewDidLoad() { 
    super.viewDidLoad() 

     yourtableview.dataSource = self 

    // Do any additional setup after loading the view. 
} 

希望這有助於

3

這是一個常見的錯誤,請確保你使用正確的話。你的情況應該是:

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

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

必須使用V IEW代替v IEW。它是區分大小寫的

相關問題