2017-06-04 85 views
0

我想在MKAnnotationView中显示几个信息,但项目的数量并不总是相同的,所以我想在这个MKAnnotationView中创建一个不可滚动的UITableView。 我没有设法做到这一点。当注释被放置时,通过点击它没有注释视图。如何在MKAnnotationView中显示UITableView?

用Joakim编辑评论: tableView显示在AnnotationView外部,并且在外部点击时不会消失。 AnnotationWithTableViewAroundTableViewDoesntDisappear

extension MapViewController: MKMapViewDelegate { 
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     let reuseIdentifier = "pin" 

     var pinView = self.mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView 
     if pinView == nil { 
      pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier) 
      let placesTableView = UITableView(frame: CGRect(x: 0, y: 0, width: 150, height: 200)) 
      placesTableView.delegate = self 
      placesTableView.dataSource = self 
      placeData = ["France", "Ile de france", "Paris"] 
      pinView?.addSubview(placesTableView) 
     } else { 
      pinView?.annotation = annotation 
     } 

     return pinView 
    } 

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     GlobalVariables.sharedInstance.selectedCountry = mapView.selectedAnnotations[0].title!! 
     self.tabBarController?.selectedIndex = 0 

     FIRAnalytics.logEvent(withName: "location_from_map", parameters: [ 
      "location": GlobalVariables.sharedInstance.selectedCountry as NSObject 
      ]) 
    } 
} 

extension MapViewController: UITableViewDelegate { 

} 

extension MapViewController: UITableViewDataSource { 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return placeData.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: nil) 
     cell.textLabel?.text = placeData[indexPath.item] 
     return cell 
    } 
} 

PS:注释观点与其他代码,我把所以我知道这个问题来自于实现代码如下实施可见。

+1

你的tableview似乎没有任何大小或位置。那是对的吗? – Starlord

+0

谢谢Joakim,我编辑了这个问题,因为它仍然没有正确显示。 – OxyFlax

+2

也许我会尝试'UIStackView' :-) – olejnjak

回答

0

问题已解决。感谢UIStackView的建议。

我的问题是事实,我是设置视图中
mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

,而我不得不这样做在
mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)

DogCoffee的回答帮了我这个职位:MapKit iOS 9 detailCalloutAccessoryView usage