Nawaf
Nawaf

Reputation: 55

UITableView inside UIViewController

I'm trying to use UITableView Inside UIViewController. However, when I tried to so it it gives me an error when I start the app. the error says "method doesnt override ant method from superclass"

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
    }

}

Upvotes: 2

Views: 1994

Answers (2)

Paolo
Paolo

Reputation: 3935

Functions involving tableView such as numberOfSections(...), cellForRowAt etc. are not part of the UIViewController.

Instead, they belong to the UITableViewDelegate and UITableViewDataSource.

To fix your errors, remove the override keyword in front of these functions. You are not overriding them but instead "implementing" them at the request of the protocols.

Be sure to also set the view controller as the delegate and dataSource if you have not already done so in a Storyboard:

override func viewDidLoad()
    super.viewDidLoad()

    // other code...

    tableView.delegate = self
    tableView.dataSource = self

    // other code...
}

Upvotes: 2

Lou Franco
Lou Franco

Reputation: 89152

When you implement the UITableViewDatasource in an UIViewController, you are not overriding the methods.

Remove override from the methods that the compiler is tell you to.

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
}

NOTE: If you were using a UITableViewController, then you would need the override, which is probably what was going on in whatever file you copied this from.

Upvotes: 4

Related Questions