2017-08-01 8 views
0

UIViewController内でUITableViewを使用しようとしています。しかし、それを試してみると、アプリを起動するとエラーになります。 エラーはUIViewControllerUITableViewDatasourceを実装するときは、メソッドをオーバーライドしていない、UIViewController内のUITableView

import UIKit 

class GPATableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    struct Objects { 
     var sectionName : String! 
     var sectionObjects : [String]! 
    } 

    var objectsArray = [Objects]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     objectsArray = 
     [Objects(sectionName: "Section1" , sectionObjects: ["","",""]), 
     Objects(sectionName: "Section2" , sectionObjects: ["","",""]), 
     Objects(sectionName: "Section3" , sectionObjects: ["","",""])] 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell! 
     // cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row] 

     print 
     return cell! 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return objectsArray[section].sectionObjects.count 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return objectsArray.count 
    } 

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
      return objectsArray[section].sectionName 
    } 

} 

答えて

1

「メソッドはスーパークラスからアリメソッドをオーバーライドdoes notの」と言います。

コンパイラが通知する方法からoverrideを削除します。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell! 
    // cell?.textLabel!.text = objectsArray[indexPath.section].sectionObjects[indexPath.row] 

    print 
    return cell! 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return objectsArray[section].sectionObjects.count 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return objectsArray.count 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return objectsArray[section].sectionName 
} 

注:UITableViewControllerを使用していた場合、あなたはおそらく、あなたがからこれをコピーしたものは何でもファイルに起こっていたされているものoverrideを、必要があるでしょう。

+0

は、あなたの答えはまた非常に役立つ、ありがとうございました。あなたが入れたノートも参考になります..もう一度ありがとう!! – Nawaf

1

tableViewnumberOfSections(...),など)を含む機能は、UIViewControllerの一部ではありません。

代わりに、それらはUITableViewDelegateUITableViewDataSourceに属します。

エラーを修正するには、これらの機能の前にoverrideというキーワードを削除してください。あなたはそれらを無効にするのではなく、プロトコルの要求でそれらを「実装する」。すでにストーリーボードにしていない場合

delegatedataSourceとしてビューコントローラを設定してください:

override func viewDidLoad() 
    super.viewDidLoad() 

    // other code... 

    tableView.delegate = self 
    tableView.dataSource = self 

    // other code... 
} 
+0

ありがとうございました。あなたの答えは非常に有用です – Nawaf

関連する問題