搜索控制器不显示搜索结果

时间:2018-06-12 23:48:50

标签: ios swift uitableview uisearchcontroller

我正在创建一个自动填充搜索栏中搜索的地图应用。但是,自动完成结果不会显示。 这是我在ViewController上使用mapView的代码:

import UIKit
import MapKit
import CoreLocation

class BarMapVC: UIViewController {

@IBOutlet weak var doneBtn: UIBarButtonItem!

@IBOutlet weak var locationBtn: UIButton!

@IBOutlet weak var barMapView: MKMapView!

var locationManager = CLLocationManager()
let authorizationStatus = CLLocationManager.authorizationStatus()
let regionRadius: Double = 1000

var resultSearchController:UISearchController? = nil

override func viewDidLoad() {
    super.viewDidLoad()

    barMapView.showsUserLocation = true
    barMapView.delegate = self
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    configureLocationServices()
    centerMapOnUserLocation()

    let locationSearchTable = 
storyboard!.instantiateViewController(withIdentifier: 
"LocationSearchTable") as! LocationSearchTable
    resultSearchController = UISearchController(searchResultsController: 
locationSearchTable)
    resultSearchController?.searchResultsUpdater = locationSearchTable

    let searchBar = resultSearchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Search for places"
    navigationItem.titleView = resultSearchController?.searchBar

    resultSearchController?.hidesNavigationBarDuringPresentation = false
    resultSearchController?.dimsBackgroundDuringPresentation = true
    definesPresentationContext = true

    locationSearchTable.mapView = barMapView
}


@IBAction func locationBtnWasPressed(_ sender: Any) {
    if authorizationStatus == .authorizedAlways || authorizationStatus 
== .authorizedWhenInUse {
        centerMapOnUserLocation()
    }
}


}

extension BarMapVC: MKMapViewDelegate {
func centerMapOnUserLocation() {
    guard let coordinate = locationManager.location?.coordinate else { 
return }
    let coordinateRegion = 
MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2.0, 
regionRadius * 2.0)
    barMapView.setRegion(coordinateRegion, animated: true)
}
}

extension BarMapVC: CLLocationManagerDelegate {
func configureLocationServices() {
    if authorizationStatus == .notDetermined {
        locationManager.requestAlwaysAuthorization()
    } else {
        return
    }
}

func locationManager(_ manager: CLLocationManager, 
didChangeAuthorization status: CLAuthorizationStatus) {
    centerMapOnUserLocation()
}

}

这是来自UITableViewController文件的代码,显示自动完成的搜索结果:

import UIKit
import MapKit

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

func parseAddress(selectedItem:MKPlacemark) -> String {
    // put a space between "4" and "Melrose Place"
    let firstSpace = (selectedItem.subThoroughfare != nil && 
selectedItem.thoroughfare != nil) ? " " : ""
    // put a comma between street and city/state
    let comma = (selectedItem.subThoroughfare != nil || 
selectedItem.thoroughfare != nil) && (selectedItem.subAdministrativeArea 
!= nil || selectedItem.administrativeArea != nil) ? ", " : ""
    // put a space between "Washington" and "DC"
    let secondSpace = (selectedItem.subAdministrativeArea != nil && 
selectedItem.administrativeArea != nil) ? " " : ""
    let addressLine = String(
        format:"%@%@%@%@%@%@%@",
        // street number
        selectedItem.subThoroughfare ?? "",
        firstSpace,
        // street name
        selectedItem.thoroughfare ?? "",
        comma,
        // city
        selectedItem.locality ?? "",
        secondSpace,
        // state
        selectedItem.administrativeArea ?? ""
    )
    return addressLine
}
}

extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
}

func updateSearchResultsForSearchController(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 {
    return matchingItems.count
}

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

我在哪里输入了错误的变量或做错了什么?我已经坚持这个错误两天了。请帮助谢谢!

0 个答案:

没有答案