2013-06-28 10 views
1

我正在創建自定義註釋,我試圖使用dequeueReusableAnnotation。 引腳之間的區別是用於引腳圖像的png。viewForAnnotation中的註釋標識符

我已經創建myAnnotation類和我使用的時候,我創建註釋此代碼:在viewForAnnotation

if([category isEqualToString:@"anti-social-behaviour"]){ 
     [email protected]"A.png"; 

    } 
    else 
     if([category isEqualToString:@"burglary"]){ 
      [email protected]"B.png"; 

     } 
     else.... 

現在:

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation 
    { 

if ([annotation class] == MKUserLocation.class) { 
    return nil; 

} 

static NSString *identifier = @"myPin"; 

MKPinAnnotationView *pinView = nil; 

pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

if (pinView == nil) 
{ 
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    pinView.canShowCallout = YES; 
    pinView.image = [UIImage imageNamed:[(Annotation*)annotation annotationImg]]; 

} 


return pinView; 

}

我知道我必須不知何故使用標識符,但我沒有弄明白。 現在的問題是,我第一次加載的引腳很好,第二個圖像弄亂了。 有什麼建議嗎?

+0

外面我應該使用大量的if/else給每個它的類別標註? – BlackM

回答

4

我會推薦在if/else塊之外實現以下代碼行,當您將MKPinAnnotationView出列時發生的情況,並且如果它返回一個視圖,那麼您只是返回仍然引用舊圖像的出列視圖。

所以你需要設置圖像的if/else之後,它應該像

if (pinView == nil) 
{ 
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    pinView.canShowCallout = YES; 
} 

pinView.image = [UIImage imageNamed:[(Annotation*)annotation annotationImg]]; 
+0

我也建議更新視圖的註釋屬性以及圖像。單獨的問題OP可能會看到圖像被替換爲默認的pin,因爲代碼創建MKPinAnnotationView而不是普通的MKAnnotationView。 – Anna