Swift - override tableview function - Method does not override any method from its superclass
Asked Answered
F

2

7

I want to implement UISearchController, going step by step using this tutorial:

Search for places using MKLocalSearchRequest and display results with UISearchController

And in this section...

Search for locations using MKLocalSearchRequest

...I have an issue with step 3.Set up the Table View Data Source

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.dequeueReusableCellWithIdentifier("cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = ""
    return cell
  }
}

Error:Method does not override any method from its superclass

I'm trying to remove override from cellForRowAtindexPath tableView function, in this case i have no error in Xcode, but have an error after execution:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'UITableView (<UITableView: 0x7ff8e03a3800; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H;
gestureRecognizers = <NSArray: 0x604002046450>; layer = <CALayer: 0x604001626620>; 
contentOffset: {0, -56}; contentSize: {375, 132};adjustedContentInset: {56, 0, 0, 0}>) 
failed to obtain a cell from its dataSource(<CityWeather.LocationSearchTable: 0x7ff8de696e60>)'

Error message yells: failed to obtain a cell from its dataSource(<CityWeather.LocationSearchTable: 0x7ff8de696e60>)

Please tell me, what I'm doing wrong? And how can I handle this issue?? Thanks.

Facelifting answered 21/11, 2017 at 12:13 Comment(3)
Please describe your Xcode or Swift version?Aristocratic
You can help yourself, it's very easy. Comment out the entire method, retype a few characters and use code completion. The tutorial you followed is most likely for Swift 2Thomasina
less copy paste and more writing!Openwork
U
14

The tutorial is using old syntax for UITableviewDatasource methods(maybe Swift 2.2):

You need these now :

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

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

}

Always use XCode's code completion wherever possible.

Ungrounded answered 21/11, 2017 at 12:19 Comment(1)
Thank you. It helped me!Facelifting
W
3

As Puneet Sharma said you do not need to override methods. Because you are using UITableViewDatasource (and UITableViewDelegate) in an UIViewController.

You need to set the delegate of the tableView in your ViewController.

Whittle answered 21/11, 2017 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.