2012-12-31 37 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