开发者

iPhone different images for different callouts

I am developing an application in iphone wh开发者_如何学编程ich deals with maps. I have many annotations. I need different images to be loaded in the callout(left accessory view) for each of these annotations. Can anyone please tell me how to do this. The user adds annotations dynamically when needed and chooses a image from the gallery to add it to that particular annotation callout. I am able to add images to callouts. but not able to differentiate it for different annotations


Set the leftCalloutAccessoryView in your viewForAnnotation: method of the Map. The leftCallout is a UIView so you can put anything there. Some sample code might look like:

        MKAnnotationView *av = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                 reuseIdentifier:@"ReuseMe"] autorelease];
        [av setCanShowCallout:YES]; 
        UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SFIcon.png"]];
        av.leftCalloutAccessoryView = sfIconView;
        [sfIconView release];  

Review the MapCallouts demo code from Apple. It does what you want.

UPDATE BASED on Comment: to change out the image just put in a new image in initWithImage. you could have the line be something like

UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArrayOfImages objectAtIndex:myIndexVariable]];

You could even refactor the lines that start UIImageView all the way to [sfIconView and pass in the image that way. The code for viewForAnnotation: gets called one time for each annotation (just like the cells in a UITableView).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜