想要爲每個註釋設置leftCalloutAccessoryView
爲不同的圖像。
leftCalloutAccessoryView
是MKAnnotationView
的一個屬性,因此您需要在viewForAnnotation
委託方法(您創建並返回MKAnnotationView
的位置)中設置它。
viewForAnnotation
委託方法獲取對在annotation
參數中創建視圖所需的註釋的引用。
因此,根據annotation
的某些屬性,您相應地設置了leftCalloutAccessoryView
。
在最原始的水平上,您可以根據annotation.title
設置leftCalloutAccessoryView
。
例如:如果標題是「證券及期貨條例」設定圖像「蘋果」,如果標題是「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;
感謝安娜,將給它在早上一試。 –