I'm adding labels just underneath my map pins to show some detail of the pin. I'm doing this by adding a UILabel as a subview of the annotationView and setting it's number of lines to 0 in order to display multiple lines on the same label. I'm having an issue where the map is initially loading the correct annotations and I can see the correct data in the labels for the map pin, but when I navigate to a different screen and then go back to the map, the map pins are all in the right places but the labels beneath them no longer show the correct data in the label, they all get mixed up. The map pins start showing data belonging to other map pins. Also if I move the map around a bit and then go back to the same pin, same thing happens.
I'm adding data to the label in the viewForAnnotation method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
// Return the view marker different for editing items ie draggable undragrable.
if ([annotation isKindOfClass:[PSMapAnnotation class]] == YES) {
PSMapAnnotation *senderAnnotation = (PSMapAnnotation *)annotation;
PSMapAnnotationView *annotationView = (PSMapAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[senderAnnotation getPinKey]];
if (annotationView == nil) {
annotationView = [[PSMapAnnotationView alloc]
initWithAnnotation:senderAnnotation
reuseIdentifier:[senderAnnotation getPinKey]];
// THIS LINE RETURNS A STRING ARRAY CONTAINING LABEL DATA
NSMutableArray *taggedAnswers = [self _getTaggedAnswersForMarkerLabel:annotationView];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
UILabel *annotationLabel = [[UILabel alloc] init];
annotationLabel.text = @"";
annotationLabel.numberOfLines = 0;
annotationLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
annotationLabel.textColor = [UIColor blackColor];
annotationLabel.textAlignment = NSTextAlignmentCenter;
// LOOP THROUGH STRING ARRAY AND ADD ALL STRINGS TO ANNOTATION LABEL
for (NSString* taggedAnswer in taggedAnswers)
{
NSString *textToShow = [NSString stringWithFormat: @"%@\n", taggedAnswer];
annotationLabel.text = [annotationLabel.text stringByAppendingString: textToShow];
}
[annotationLabel sizeToFit];
annotationLabel.center = CGPointMake(annotationView.center.x, annotationView.center.y);
annotationView.myLabel = annotationLabel;
[annotationView addSubview:annotationLabel];
[annotationView setAnnotation:annotation];
}
else {
[annotationView setAnnotation:annotation];
}
return annotationView;