简体   繁体   English

如何在MKAnnotationView中显示UITableView?

[英]How to show a UITableView in MKAnnotationView?

I would like to show several information in MKAnnotationView but the number of items is not always the same so I would like to make a non-scrollable UITableView in this MKAnnotationView. 我想在MKAnnotationView中显示一些信息,但是项的数量并不总是相同的,因此我想在此MKAnnotationView中创建不可滚动的UITableView。 I didn't managed to do that properly. 我没有正确地做到这一点。 When the annotation is placed, there is no annotation view by clicking on it. 放置注解后,单击便没有注解视图。

Edit with Joakim comment: The tableView is showed outside the AnnotationView and it doesn't disappear when click outside. 使用Joakim注释进行编辑:tableView显示在AnnotationView的外部,并且在外部单击时不会消失。 AnnotationWithTableViewAround TableViewDoesntDisappear

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: The annotation view is visible with the other code I put so I know the problem comes from the tableview implementation. PS:注释视图与其他代码一起可见,因此我知道问题出在表视图实现中。

Problem solved. 问题解决了。 Thanks for the UIStackView advice. 感谢您的UIStackView建议。

My problem was in fact that I was setting the view in 我的问题实际上是在设置视图
mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

while I had to do it in 而我不得不这样做
mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) . mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)

DogCoffee's answer helped me on this post: MapKit iOS 9 detailCalloutAccessoryView usage DogCoffee的答案在这篇文章上对我有所帮助: MapKit iOS 9 detailCalloutAccessoryView用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM