繁体   English   中英

如何在MKAnnotationView中显示UITableView?

[英]How to show a UITableView in MKAnnotationView?

我想在MKAnnotationView中显示一些信息,但是项的数量并不总是相同的,因此我想在此MKAnnotationView中创建不可滚动的UITableView。 我没有正确地做到这一点。 放置注解后,单击便没有注解视图。

使用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:注释视图与其他代码一起可见,因此我知道问题出在表视图实现中。

问题解决了。 感谢您的UIStackView建议。

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

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

DogCoffee的答案在这篇文章上对我有所帮助: MapKit iOS 9 detailCalloutAccessoryView用法

暂无
暂无

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

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