2012-12-31 79 views
2

您好,感謝您的幫助。ios:MapView引腳顏色註釋不斷變化;爲什麼?

我使用下面的代碼來設置我的MapView註釋上的針顏色?

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




    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"]; 
    if (!pinView) { 

     ////////////// 
     pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"]; 
     pinView.pinColor = MKPinAnnotationColorGreen; 
     if([[annotation title] isEqualToString:@"MuRoom"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorRed; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Mike's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorRed; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Bill's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorPurple; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Steve's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorGreen; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Louisa's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorGreen; 

      NSLog(@"data from ann index %@", annTile); 
     } 


     pinView.animatesDrop = YES; 
     pinView.canShowCallout = YES; 

     UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     pinView.rightCalloutAccessoryView = rightButton; 
    } else { 
     pinView.annotation = annotation; 
    } 
    return pinView; 
} 


I am then using the function below to filter my pins locaions 



-(void)FilterAddAll:(id)sender 
{ 
    [mapview removeAnnotations:annoArra]; 
    [mapview removeAnnotations:annoArrayVenue]; 
[mapview removeAnnotations:artArray]; 


// CLLocationCoordinate2D center = mapview.centerCoordinate; 
// mapview.centerCoordinate = center; 


    [mapview addAnnotations:annoArra]; 
    [mapview addAnnotations:annoArrayVenue]; 
    [mapview addAnnotations:artArray]; 

} 

-(void)FilterArt:(id)sender 
{ 
    [mapview removeAnnotations:annoArra]; 
    [mapview removeAnnotations:artArray]; 
    [mapview removeAnnotations:annoArrayVenue]; 

    [mapview addAnnotations:annoArra]; 
} 

-(void)FilterVenue:(id)sender 
{ 
    [mapview removeAnnotations:annoArra]; 
    [mapview removeAnnotations:artArray]; 
    [mapview removeAnnotations:annoArrayVenue]; 

    [mapview addAnnotations:artArray]; 
} 

問題:如何獲得銷釘顏色以保留其原有顏色?過濾後,他們回來作爲隨機的針顏色。

再次感謝。

回答

4

發生這種情況是因爲您沒有正確使用「reuseIdentifier」。當你回到從dequeueReusableAnnotationViewWithIdentifier引腳:@「pinView」,您需要:

總是設置引腳顏色,或者 使用不同的reuseIdentifier的每種顏色的針

即你可能會得到一個可重複使用查看與紅色的大頭針,而且要顯示一個藍色圖釘

例如:

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"]; 
    if (!pinView) { 

     ////////////// 
     pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"]; 

     pinView.animatesDrop = YES; 
     pinView.canShowCallout = YES; 

     UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     pinView.rightCalloutAccessoryView = rightButton; 
    } else { 
     pinView.annotation = annotation; 
    } 

// SET THE PIN COLOR REGARDLESS OF WHETHER A REUSABLE ANNOTATION WAS RETURNED OR NOT 

     pinView.pinColor = MKPinAnnotationColorGreen; 
     if([[annotation title] isEqualToString:@"MuRoom"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorRed; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Mike's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorRed; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Bill's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorPurple; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Steve's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorGreen; 

      NSLog(@"data from ann index %@", annTile); 
     } 
     if([[annotation title] isEqualToString:@"Louisa's"]) 
     { 
      // Do somethingMKAnnotation 
      pinView.pinColor = MKPinAnnotationColorGreen; 

      NSLog(@"data from ann index %@", annTile); 
     } 
+0

無知的霧慢慢擡起。再次感謝。 – AhabLives