ios - 如何在 MKAnnotationView 中显示 UITableView?

标签 ios swift uitableview mapkit mkannotationview

我想在 MKAnnotationView 中显示一些信息,但项目的数量并不总是相同,所以我想在此 MKAnnotationView 中制作一个不可滚动的 UITableView。 我没能正确地做到这一点。放置注释后,点击注释不会出现注释 View 。

编辑 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:注释 View 与我放置的其他代码一起可见,因此我知道问题来自 TableView 实现。

最佳答案

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

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

虽然我必须这样做
mapView(_mapView: MKMapView, didSelect View : MKAnnotationView).

DogCoffee 的回答对我这篇文章有帮助:MapKit iOS 9 detailCalloutAccessoryView usage

关于ios - 如何在 MKAnnotationView 中显示 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353225/

相关文章:

ios - 使用 CocoaPods 找不到 Firebase.h 文件

ios - 适用于 iPhone X 的原始深度图 SDK

ios - 快速调用中的额外参数

ios - 如何循环遍历 UITableView 中尚未可见的单元格?

ios - 暂时禁用 UITableView 中的删除按钮

ios - Swift 和 CoreData/数据存储

ios - 谁能帮我解决这个 xcode 问题?

swift - .font 不可转换为 .font

ios - 从另一个 VC 打开一个 VC,而不将它们推送到导航堆栈 (Swift)

iphone - 如何设置 TableView 单元格附件 View 以保留先前初始化的 UIImageView?