0

I am trying to load the updated search results but it doesn't populate the table view.

I used this link https://www.thorntech.com/how-to-search-for-location-using-apples-mapkit/ which belongs to the previous versions but it still works very well except showing the local search results.

class LocationSearchTable : UITableViewController, UISearchResultsUpdating {
    var matchingItems:[MKMapItem] = []
    var mapView: MKMapView? = nil
    
}

extension LocationSearchTable {
    func updateSearchResults(for searchController: UISearchController) {
        guard let MapView = mapView,
            let searchBarText = searchController.searchBar.text else { return }
        let request = MKLocalSearch.Request()
        request.naturalLanguageQuery = searchBarText
        request.region = MapView.region
        let search = MKLocalSearch(request: request)
        search.start { response, _ in
            guard let response = response else {
                print("No response")
                return
            }
            self.matchingItems = response.mapItems
            self.tableView.reloadData()
        }
    }
}

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

    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
    }
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
  • Have you stepped through in the debugger to identify where the problem lies? You should also not drop the completion handler's error inout as that might tell you what's wrong. – flanker Sep 20 '21 at 14:39
  • @flanker i have stepped through in the debugger unfortunately i couldn't identify the problem. I am new to swift so i haven't understood how the completion handler works. I just followed the steps of the documentation. – Nusret Ali Kızılaslan Sep 20 '21 at 21:00
  • My code differs from the documentation in the second extension's second method. In the documentation, method is declared as 'override func' but when I declared it as 'override func', Xcode gives me an error "Method does not override any method from its superclass" which makes sense. I don't know if it has any connection with the issue. – Nusret Ali Kızılaslan Sep 20 '21 at 21:21

1 Answers1

0
 //use IndexPath rather than NSIndexPath and you need to use 
 //override

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

Hope it is not too late to answer you!

ho jasper
  • 1
  • 1