2016-08-19 38 views
1

MapViewを使用しているアプリケーションで作業していますが、今問題を見つけやすくなりました。iOSのテーブルビューでlocalsearchresponseデータを表示できません

  • ユーザーが場所の名前を入力すると、入力欄に基づいて候補が表示されます。

  • 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 
} 

}

+0

あなたはuitableview delegateを設定していませんか? –

+0

申し訳ありませんが、UITableViewDelegateを追加した後でも、私はLocalSearchResponseを表ビューのセルに表示できません。 –

+0

tableview cellForRowAtIndexPath関数にブレークポイントを置くだけです。それを呼び出すかどうかを確認してください。 –

答えて

0
  1. 適切は画像
  2. 、デリゲートを追加し、datasuorce両方UITableViewDataSourceのようなコンマでseprating、UITableViewDelegateで見るストーリーボードを使用して、関連viewControllerからdelegateとごtableViewdatasourceを設定あなたのクラス。

enter image description here

今、あなたは、プログラム内のデリゲートを設定する必要がいけません。

  • コールreloadData()あなたはそれに応じて、単一の単語またはを入力すると、各と毎回機能。
+0

あなたは私の問題を解決しましたありがとうございます。 –

+0

ユーザーが再度検索するたびにテーブルビューセルの内容を消去する方法はありますか、ユーザーが入力中か終了済みかを確認する方法はありますか。私は検索バーではなく、バーボタンの項目を使用しています。 –

+0

@ GuruTejaあなたは 'shouldChangeCharactersInRange' [** delegate method **](http://code.tutsplus.com/tutorials/ios-sdk-uitextfield-uitextfielddelegate--mobile-10943)の' uitextfield '.. – vaibhav

関連する問題