我想在 MKAnnotationView 中显示一些信息,但项目的数量并不总是相同,所以我想在此 MKAnnotationView 中制作一个不可滚动的 UITableView。 我没能正确地做到这一点。放置注释后,点击注释不会出现注释 View 。
编辑 Joakim 评论: tableView显示在AnnotationView之外,并且当点击外部时它不会消失。
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/