Computer Science, asked by ashi7444, 1 year ago

Uitableview failed to obtain a cell from its datasource swift

Answers

Answered by chintu123456789
0
I have the below UITableViewController attached as a searchResultsController:

import UIKit import MapKit class LocationSearchTable : UITableViewController { var matchingItems:[MKMapItem] = [] var mapView: MKMapView? = nil override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self } } extension LocationSearchTable : UISearchResultsUpdating { func updateSearchResults(for searchController: UISearchController){ guard let mapView = mapView, let searchBarText = searchController.searchBar.text else { return } let request = MKLocalSearchRequest() request.naturalLanguageQuery = searchBarText request.region = mapView.region let search = MKLocalSearch(request: request) search.start { response, _ in guard let response = response else { return } self.matchingItems = response.mapItems self.tableView.reloadData() } } } extension LocationSearchTable { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print(matchingItems.count) 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 } }
Similar questions