简体   繁体   中英

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
    }

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM