2

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
    }

}

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much , your answer also very helpful. also the Note that you put is helpful.. thanks again !!
2

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...
}

1 Comment

Thank you very much. your answer is very helpfull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.