報錯-Type ViewController doesnot conform to protocol

UPDATE: 2015/12/06 Updated for Xcode 7 and Swift 2..net

報錯內容:

Type 'ViewController' does not conform to protocol 'UITableViewDataSource'code

復現步驟:

(1) 在ViewController 鍵入UITableViewDataSource, UITableViewDelegate兩個協議,讓ViewController 繼承這個兩個協議orm

(2) Xcode當即報錯:Type 'ViewController' does not conform to protocol 'UITableViewDataSource'blog

問題緣由:

在使用UITableViewDataSource, UITableViewDelegate兩個協議時,必需要實現幾個tableView方法繼承

解決辦法:

在ViewController中實現一下三個方法:get

當實現了前兩個方法後,Xcode就不提示這個錯誤了。io

// 返回分組數
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    <#code#>
}

// 根據分組,返回每一個分組的行數
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    <#code#>
}

// 根據分組,返回每一個cell
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    <#code#>
}

引用極客學院的技術問答:table

當打入UITableViewDataSource,UITableViewDelegate兩個協議時,一直提示 「type‘ViewController’ does not conform to protocol 'UITableViewDataSource'」但願教授一下,這倆協議應該如何用?form

極客學院-silence 03月27日 回答 #1樓 這兩個協議分別都有幾個方法,是必須繼承的,若是你沒有繼承,就會提示這個問題,表明你沒有實現這個協議date

參考:

http://wenda.jikexueyuan.com/question/10160/
http://blog.csdn.net/mjbaishiyun/article/details/42580729

相關文章
相關標籤/搜索