簡體   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