leftCalloutAccessoryView注釈ごとに異なる画像 に設定したいとします。
はのleftCalloutAccessoryViewプロパティであるため、デリゲート メソッド ( を作成して返す場所)MKAnnotationViewで設定する必要があります。viewForAnnotationMKAnnotationView
viewForAnnotationデリゲート メソッドは、パラメーターでビューを作成する必要がある注釈への参照を取得しますannotation。
したがって、 のいくつかのプロパティに基づいて、それに応じannotationて設定しますleftCalloutAccessoryView。
leftCalloutAccessoryView最も粗いレベルでは、に基づいて
設定できますannotation.title。
例: タイトルが「SFO」の場合は画像を「りんご」に設定し、タイトルが「ATL」の場合は画像を「桃」に設定します。
MKAnnotationただし、注釈に使用する画像を明確に示す
別のプロパティを ( を実装する注釈クラスで) 作成する方がはるかに優れています。このプロパティは、UIImageそれ自体、画像の名前、数値など、状況に最適なものであれば何でもかまいません。
注釈を作成するとき、および を呼び出す前にaddAnnotation、注釈のこのプロパティを設定します。
次に、viewForAnnotationデリゲート メソッドでleftCalloutAccessoryView、カスタム アノテーション プロパティに基づいて を設定します。
たとえば、次のNSString名前のプロパティimageNameがアノテーション クラスに追加されたとします。
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;