I would like to show several information in MKAnnotationView but the number of items is not always the same so I would like to make a non-scrollable UITableView in this MKAnnotationView. I didn't managed to do that properly. When the annotation is placed, there is no annotation view by clicking on it.
Edit with Joakim comment:
The tableView is showed outside the AnnotationView and it doesn't disappear when click outside.
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: The annotation view is visible with the other code I put so I know the problem comes from the tableview implementation.