Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在ios的表视图中显示localsearchresponse数据_Ios_Swift_Tableview_Mklocalsearch - Fatal编程技术网

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

无法在ios的表视图中显示localsearchresponse数据,ios,swift,tableview,mklocalsearch,Ios,Swift,Tableview,Mklocalsearch,我正在使用MapView开发一个应用程序,现在有一个问题,请帮助我解决 当用户键入一个地方的名称时,我放置了一个搜索栏,然后它根据输入的字符串给出建议 使用mapkit,我能够向默认地图发送查询,接收MKLocalSearchResponse 现在我的问题是来自LocalSearchResponse的数据没有显示在tableView单元格中 请提供解决方案 使用工具:Xcode 7.3、swift 2.2 这是我的代码,请看一下 import UIKit import MapKit clas

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

  • 当用户键入一个地方的名称时,我放置了一个搜索栏,然后它根据输入的字符串给出建议

  • 使用mapkit,我能够向默认地图发送查询,接收MKLocalSearchResponse

现在我的问题是来自LocalSearchResponse的数据没有显示在tableView单元格中

请提供解决方案

使用工具:Xcode 7.3、swift 2.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
}
}

  • 使用序列图像板将
    表格视图的
    委托
    数据源
    设置为关联的
    视图控制器
  • 在类中添加delegate和datasuorce,并使用逗号分隔,如
    UITableViewDataSource
    UITableViewDelegate
  • 现在,不需要在程序中设置委托

    • 每次键入单个单词或相应的单词时都调用reloadData()函数
  • 使用序列图像板将
    表格视图的
    委托
    数据源
    设置为关联的
    视图控制器
  • 在类中添加delegate和datasuorce,并使用逗号分隔,如
    UITableViewDataSource
    UITableViewDelegate
  • 现在,不需要在程序中设置委托

    • 每次键入单个单词或相应的单词时都调用reloadData()函数
    您没有设置uitableview委派?很抱歉,但是,即使在添加UITableViewDelegate之后,我也无法在表视图单元格中显示LocalSearchResponse。只需在tableview单元格中为RowatineXpath函数设置一个断点即可。请检查它调用了什么,或者是否调用了名为check matchingItems数组的函数是否具有与设置委托相同的值。数据源负责调用numberOfRowsInSection、cellForRowAtIndex等。因此,如果其为零,则不会调用任何数据源方法。您没有设置uitableview委托?很抱歉,但是,即使在添加UITableViewDelegate之后,我也无法在表视图单元格中显示LocalSearchResponse。只需在tableview单元格中为RowatineXpath函数设置一个断点即可。请检查它调用了什么,或者是否调用了名为check matchingItems数组的函数是否具有与设置委托相同的值。数据源负责调用numberOfRowsInSection、CellForRowatinex等。因此,如果其为零,则不会调用任何数据源方法。是否有任何方法可以在用户每次再次搜索时清除表视图单元格内容,以及如何查找用户是否正在键入、是否已完成或已取消。我使用的是bar按钮项而不是搜索栏。@GuruTeja您可以使用
    uitextfield
    shouldChangeCharactersRange
    来检测用户键入。为什么不能使用func textField shouldEndediting(textField:uitextfield)->Bool.是否有任何方法可以在用户每次再次搜索时清除表视图单元格内容,以及如何查找用户是否正在键入、是否已完成或是否已取消。我使用的是bar按钮项而不是搜索栏。@GuruTeja您可以通过使用
    uitextfield
    shouldChangeCharactersRange
    来检测用户键入。为什么我们不能使用func textField shouldediting(textField:uitextfield)->Bool。