James M
James M

Reputation: 41

Method Does Not Override Any Method From Its Superclass: Swift/UITableViewController/MapKit

I'm creating a mapView with an autocomplete search bar, and I'm getting a Method Does Not Override Any Method From Its Superclass error. Here is my sample of code where this error occurs:

extension LocationSearchTable {
override func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int {
    return matchingItems.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath 
indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = ""
    return cell
}
}

This is the function that is creating an error:

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

What am I doing wrong? Thanks!

Upvotes: 0

Views: 1938

Answers (2)

bobo pei
bobo pei

Reputation: 52

If you want to add a tableView to a view, you should set the tableview's delegate and datasource to a object, and implement method of datasource and delegate if you need.

Upvotes: 0

OOPer
OOPer

Reputation: 47896

You may need to clarify some things.

I assume:

  • Your LocationSearchTable is a subclass of UITableViewController.

  • You use Xcode 8/Swift 3 or later.

  • You do not get errors or warnings on the line: override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

And you should better show where you get that code showing the error, it seems very, very old.

Better find how the methods of UITableViewDataSource looks like in the latest Swift, which, you know, UITableViewController conforms:

UITableViewDataSource

...

func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell

...

Imported method names are more swiftified in recent Swifts, and you should be careful when using old code fragments.

Try changing the method header to:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

Please do not forget the empty label _ for the first parameter, and the second parameter label is cellForRowAt, not cellForRowAtIndexPath, and its type is IndexPath, not NSIndexPath.

And one more, you may get an error if you remove override.

Upvotes: 1

Related Questions