您想为每个注释将leftCalloutAccessoryView 设置为不同的图像。
leftCalloutAccessoryView 是MKAnnotationView 的一个属性,因此您需要在viewForAnnotation 委托方法中设置它(这是您创建并返回MKAnnotationView 的地方)。
viewForAnnotation 委托方法获取对注释的引用,您需要在 annotation 参数中为其创建视图。
因此,根据annotation 的某些属性,您可以相应地设置leftCalloutAccessoryView。
在最粗略的水平上,您可以根据annotation.title 设置leftCalloutAccessoryView。
例如:如果标题为“SFO”,则将图像设置为“苹果”,如果标题为“ATL”,则将图像设置为“桃子”等。
但是,最好创建一个单独的属性(在实现MKAnnotation 的注释类中)清楚地指示要用于注释的图像。该属性可以是UIImage 本身、图像的名称、数字等——任何最适合您的情况。
在创建注解时和调用addAnnotation之前,你设置注解的这个属性。
然后在viewForAnnotation委托方法中,根据自定义注解属性设置leftCalloutAccessoryView。
例如,假设一个名为imageName 的NSString 属性被添加到注解类中:
MKAnnotationView *av = ... //or MKPinAnnotationView
//typical dequeue and alloc/init code here
if ([annotation isKindOfClass:[MyAnnotationClass class]])
{
//cast the annotation parameter to your custom class
//so you can easily access the custom properties...
MyAnnotationClass *myAnn = (MyAnnotationClass *)annotation;
//create UIImage based on custom property of annotation...
UIImage *img = [UIImage imageNamed:myAnn.imageName];
//create UIImageView to use for the leftCalloutAccessoryView...
UIImageView *iv = [[[UIImageView alloc] initWithImage:img] autorelease];
//if using ARC, remove the autorelease above
av.leftCalloutAccessoryView = iv;
}
return av;