5

I have a MapView with cluster annotations (ADMapCluster). I want to show the amount of elements in the Cluster, therefor I'm adding a UILabel to the MKAnnotationView as a Subview.

My Problem is that when I reuse the MKAnnotationView after zooming or similar actions the UILabel doesn't update the text.

- (MKAnnotationView *)mapView:(ADClusterMapView *)mapView viewForClusterAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

MKAnnotationView * pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ADMapCluster"];
if (!pinView) {
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ADMapCluster"];
    pinView.image = [UIImage imageNamed:@"map-markers"];
    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 5, 25, 20)];
    countLabel.textAlignment = NSTextAlignmentCenter;
    [countLabel setTag:2];
    countLabel.text = [NSString stringWithFormat:@"%d",[[(ADClusterAnnotation*)pinView.annotation originalAnnotations] count] ];
    countLabel.font = [UIFont fontWithName:@"Avenir-Book" size:10];
    [pinView addSubview:countLabel];

}
else {
    pinView.annotation = annotation;
    [((UILabel*)[pinView viewWithTag:2]) setText:[NSString stringWithFormat:@"%d",[[(ADClusterAnnotation*)pinView.annotation originalAnnotations] count] ]];
    [((UILabel*)[pinView viewWithTag:2]) setNeedsDisplay];
}
return pinView;

}

Any idea what I'm doing wrong and why the labels don't get updated?

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
codewitch
  • 73
  • 5
  • 1
    Are you sure about result of **[(ADClusterAnnotation*)pinView.annotation originalAnnotations] count] ]**..? – Kumar KL Sep 30 '13 at 13:04
  • Not an answer but the `setText` line in the `else` part would be so much easier to read, maintain, and (most importantly) to debug if it was split up into multiple statements using local variables to hold refs to the objects that line is accessing. Then you can easily check if something is nil or some unexpected value. –  Sep 30 '13 at 13:55
  • @KumarKl Yes, you are totally right. The issue is NOT that the labels are not getting updated. Im getting wrong numbers. Thanks a lot! – codewitch Sep 30 '13 at 15:01
  • Hmmm.... small mistakes make mad ... – Kumar KL Oct 01 '13 at 05:11

0 Answers0