0

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. AnnotationWithTableViewAroundTableViewDoesntDisappear

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.

OxyFlax
  • 451
  • 1
  • 5
  • 16
  • 1
    Your tableview doesn't seem to have any size or position. Is that correct? – JoakimE Jun 04 '17 at 11:02
  • Thank you Joakim, I edited the question as it still is not displayed properly. – OxyFlax Jun 04 '17 at 11:24
  • 2
    Maybe I would try `UIStackView` :-) – olejnjak Jun 04 '17 at 12:11
  • We can't make StackView with not known number of items, can't we? – OxyFlax Jun 04 '17 at 12:28
  • 1
    Sure you can. Just create stack view and then add however many arranged views you need for that particular annotation view. Stack view is much better suited for this scenario than table view. – Rob Jun 04 '17 at 13:12
  • Even if I want to be able to click on the element I choose and get all the data before? For example I click on Paris, I got "Paris/Ile de France/France", I click on Ile de France, I got "Ile de france/Paris" ? With tableView I can just get the subArray with indexPath of item clicked. – OxyFlax Jun 04 '17 at 13:22
  • 1
    Sure. You can make your subviews with user interaction enabled, and then then have tap gesture recognizers on them. – Rob Jun 04 '17 at 13:25
  • You said "When the annotation is placed, there is no annotation view by clicking on it." - I'm not sure what you mean because the the annotation view is the thing you click on. Or do you mean that there's no callout when you click on the annotation view? And what are your two screen snapshots? One has a "France" bubble and the other doesn't. I don't understand the desired UX. – Rob Jun 04 '17 at 13:26
  • I would like the view to be in the annotationView (where we can see "France"). But for the moment, it appears outside (see Edit, at beginning it didn't appeared at all, but now it appears outside). And when I tap outside the view, the tableview is still here while it should disappear like the other one. – OxyFlax Jun 04 '17 at 13:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145871/discussion-between-oxyflax-and-rob). – OxyFlax Jun 05 '17 at 11:40

1 Answers1

0

Problem solved. Thanks for the UIStackView advice.

My problem was in fact that I was setting the view in
mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

while I had to do it in
mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView).

DogCoffee's answer helped me on this post: MapKit iOS 9 detailCalloutAccessoryView usage

OxyFlax
  • 451
  • 1
  • 5
  • 16