Swift: Table view superclass error

℡╲_俬逩灬. 提交于 2019-12-01 12:21:06

The method does not override any method of the superclass because the signature is wrong.

The correct signature is

override func tableView(tableView: UITableView, 
  cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

All parameters are non-optional types.

And use also the recommended method

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,
                                           forIndexPath: indexPath)

which also returns always a non-optional type

Make sure that your class implements UITableViewDelegate and UITableViewDataSource.

class MyController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // All your methods here
}

You won't need override keyword unless any other superclass already implements those methods.

See this works for me

  • Just confirm UITableViewDelegate and UITableViewDataSource
  • Implement required methods

import UIKit

class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    let containArray = ["One","two","three","four","five"]


    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: Table view data source and delegate methods

    //Number of rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return containArray.count
    }

    //Prepare Custom Cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

        let identifier = "simpleTextCell"
        var cell: SimpleTextCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? SimpleTextCell

        if cell == nil {
            tableView.registerNib(UINib(nibName: "SimpleTextCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? SimpleTextCell
        }

        let dayName = containArray[indexPath.row]
        cell.lblProductName.text = dayName


        return cell
    }

    //Handle click
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("productListSegue", sender: self)
    }
}

Try to removing "!", declaration for this function is:

 func tableView(_ tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

and make sure you have set delegate and datasource of tableview to "self"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!