【问题标题】:unable to show localsearchresponse data in table view in ios无法在 ios 的表视图中显示 localsearch 响应数据
【发布时间】:2016-08-19 10:02:48
【问题描述】:

我正在使用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
}

}

【问题讨论】:

  • 你没有设置uitableview委托?
  • 很抱歉,但即使添加 UITableViewDelegate 我也无法在表格视图单元格中显示 LocalSearchResponse。
  • 只需在 tableview cellForRowAtIndexPath 函数中放置一个断点。检查它是否调用
  • 如果那个叫做检查匹配项数组的函数有任何值
  • 设置委托的方式相同。数据源负责 numberOfRowsInSection、cellForRowAtIndex 等的调用。因此,如果它为 nil,则不会调用任何数据源方法。

标签: ios swift tableview mklocalsearch


【解决方案1】:
  1. 使用情节提要正确地将tableViewdelegatedatasource 设置为关联的viewController查看图片
  2. 在您的类中添加委托和数据源,两者都用逗号分隔,例如 UITableViewDataSourceUITableViewDelegate

现在你不需要在程序中设置委托了。

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

【讨论】:

  • 每次用户再次搜索时,是否有任何方法可以清除表格视图单元格内容以及如何查找用户是否正在输入、完成或取消。我使用的是栏按钮项而不是搜索栏。
  • @GuruTeja 你可以通过使用shouldChangeCharactersInRange delegate method of uitextfield 来检测用户输入..
  • 为什么我们不能使用 func textFieldShouldEndEditing(textField: UITextField) -> Bool.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2023-04-08
  • 2023-03-02
  • 1970-01-01
  • 2019-02-10
相关资源
最近更新 更多