无法在 ios 的 table 视图中显示 localsearchresponse 数据

unable to show localsearchresponse data in table view in ios

我正在开发一个使用 MapView 的应用程序,现在有问题请帮助我解决。

Now My problem is data from LocalSearchResponse is not getting displayed in tableView cells.

请提供解决方案。

使用工具:Xcode7.3,swift2.2

这是我的代码,请看一下。

import UIKit
import MapKit
class ViewController: UIViewController, UISearchBarDelegate,UITableViewDelegate {

var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var items1:String = ""

var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!
var matchingItems: [MKMapItem] = [MKMapItem]()
var mapItems: [MKMapItem] = [MKMapItem]()
var itm : [String] = []

@IBAction func showSearchBar(sender: AnyObject) {
    searchController = UISearchController(searchResultsController: nil)
    searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.searchBar.delegate = self
    presentViewController(searchController, animated: true, completion: nil)

}

@IBOutlet var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

    func searchBarSearchButtonClicked(searchBar: UISearchBar){

    searchBar.resignFirstResponder()
    dismissViewControllerAnimated(true, completion: nil)
    if self.mapView.annotations.count != 0{
        annotation = self.mapView.annotations[0]
        self.mapView.removeAnnotation(annotation)
    }

    localSearchRequest = MKLocalSearchRequest()
    localSearchRequest.naturalLanguageQuery = searchBar.text
    localSearch = MKLocalSearch(request: localSearchRequest)
    localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

        if localSearchResponse == nil{
            let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alertController, animated: true, completion: nil)
            return
        }

        for item in localSearchResponse!.mapItems {

            print("Name = \(item.name)")
            self.items1 = item.name!

            self.matchingItems.append(item as MKMapItem)
            print("Matching items = \(self.matchingItems.count)")
                }
        self.pointAnnotation = MKPointAnnotation()
        self.pointAnnotation.title = searchBar.text
        self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:     localSearchResponse!.boundingRegion.center.longitude)


        self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
        self.mapView.centerCoordinate = self.pointAnnotation.coordinate
        self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
    }

}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let entry = matchingItems[indexPath.row]
cell.textLabel!.text = items1[indexPath.row]

return cell
}

}

  1. 正确使用故事板将 tableViewdelegatedatasource 设置为关联的 viewController 见图
  2. 在你的 class.
  3. 中添加 delegate 和 datasuorce 都用逗号分隔,如 UITableViewDataSource, UITableViewDelegate

now you dont need to set the delegate inside the program.

  • 每次键入单个单词或相应地调用 reloadData() 函数。