2

I've managed to draw numbers inside custom colored circle annotations (based on this). I want to make some optimizations for my custom annotation class and I read about reusing. My problem is if I make the stuff reusable, the annotation views get mixed on map which is a big problem. The custom drawn annotation views cannot be reused ? Or is it somehow related to the view's annotaion ? I mean, the annotation stores the number to be drawn on its view, practically it's a 1to1 relationship between annotation and its view.

Here is my relevant code : Custom annotationview's init :

-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageType:(int)imageType {

    self = [super initWithAnnotation: annotation reuseIdentifier: reuseIdentifier];
    if (self != nil)
    {
       if ([annotation isKindOfClass:[CircleMarker class]]) 
       {
           // custom annotation class with some extra fields
           CircleMarker * clm = (CircleMarker * )annotation;
           self.locationMarker = clm; 

           //  ... setting frame and other stuff

           self.image = [self getImage];  /* this method DRAWS image based on clm */
           self.canShowCallout = NO;           
       }
...
}

And the delegate :

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    static NSString *reuseId_small = @"smallcircle"; 
    static NSString *reuseId_big   = @"bigcircle";   
    CircleAnnotationView * nca = nil;    
    if ((int)[self.mapView getZoomLevel] < ZOOM_LEVEL_FOR_NUMBERS)
        {
        nca = (CircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_small];   
        if (nca == nil )
            nca = [[[CircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_small imageType:2] autorelease];
        }
        else 
        {
        nca = (CircleAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId_big];  
        if ( nca == nil )
            nca = [[[CircleAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId_big imageType:1] autorelease];
        }
 return nca;
}

I've tried to replace the self.image = part with a custom drawRect function, but the result was the same.

Thanks.

Community
  • 1
  • 1
Templar
  • 1,694
  • 1
  • 14
  • 32
  • Or, in these cases only the colored circles (the circle's background) can be reused and I should separate the drawn number from the background ? – Templar Feb 29 '12 at 08:37

1 Answers1

2

There is a call to prepareForReuse on the MKAnnotationView when it gets dequeued. In this method you could check to see if the content needs redrawing.

https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html

I see this going inside your CircleAnnotationView code

Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
  • Yeah, probably this is the best answer. After all I ended to separate the common background to be reusable, so I draw the content on a transparent circle which I put on the reused circles. Thanks anyway. – Templar Mar 16 '12 at 12:01