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?

4

0 回答 0