I've got an array of places. Each of these places are displayed on a MapView and on a TableView on the same screen. I'd like to pop up the pin annotation of a place when I tap the corresponding row on the table view.
I've got the following code to populate annotations:
for (int i = 0; i < [self.myPlaces count]; i++)
{
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
SPlaces *ev = [self.myPlaces objectAtIndex:i];
point.coordinate = CLLocationCoordinate2DMake([ev.location.latitude doubleValue], [ev.location.longitude doubleValue]);
point.title = ev.title;
point.subtitle = ev.type.name;
[self.myMap addAnnotation:point];
}
and this code to display an annotation when a row is selected on the TableView:
//row = tapped row on TableView
[self.myMap selectAnnotation:[[self.myMap annotations] objectAtIndex:row] animated:YES];
The issue is that the annotations seems to have a random index in the myMap annotations array. I mean, i'd like to have this behavior:
users taps row 0 on Table view => annotation of pin 0 pops up on the map
users taps row 1 on Table view => annotation of pin 1 pops up on the map
users taps row 2 on Table view => annotation of pin 2 pops up on the map
But instead of this, I get:
users taps row 0 on Table view => annotation of pin 1 pops up on the map
users taps row 1 on Table view => annotation of pin 0 pops up on the map
users taps row 2 on Table view => annotation of pin 2 pops up on the map
...or any other random combination that change each time I restart the app. Note that if I enable user's location, one of my pin annotation can have it's title randomly changed for "Current location".
I'm pretty sure I'm note populating or accessing the annotation correctly. Do you have any clue ?
Thanks !
EDIT: This post seems to say that you have to scan your annotations array, looking for a matching title, then select the relevant annotation : Open Map Annotation from Selected Row in Table View Is this really the only way ? Since two pins could possibly share the same title, it seems a little bit hazardous...