我想在MKAnnotationView中显示几个信息,但项目的数量并不总是相同的,所以我想在这个MKAnnotationView中创建一个不可滚动的UITableView。 我没有设法做到这一点。当注释被放置时,通过点击它没有注释视图。如何在MKAnnotationView中显示UITableView?
用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:注释观点与其他代码,我把所以我知道这个问题来自于实现代码如下实施可见。
你的tableview似乎没有任何大小或位置。那是对的吗? – Starlord
谢谢Joakim,我编辑了这个问题,因为它仍然没有正确显示。 – OxyFlax
也许我会尝试'UIStackView' :-) – olejnjak