针脚颜色未定期显示为预期

时间:2014-03-18 23:47:19

标签: ios annotations mapkit mkpinannotationview

在我的问题Detecting selected annotation to change pin color的答案的第一部分之后,我更新了我的代码如下:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    //7
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //8
    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {
        //9
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        {
            myAnnotation *myAnn = (myAnnotation *)annotation;
            NSLog(@"ANNOTATION GRUPO = %d",myAnn.grupo);
            switch (myAnn.grupo)
            {
                case 1:
                    annotationView.pinColor = MKPinAnnotationColorRed;
                    break;
                case 2:
                    annotationView.pinColor = MKPinAnnotationColorGreen;
                    break;
                case 3:
                    annotationView.pinColor = MKPinAnnotationColorPurple;
                    break;
                default:
                    annotationView.pinColor = MKPinAnnotationColorPurple;
                    break;
            }
        }
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;

    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}

而changeOpcion方法如下:

- (IBAction)changeOpcion:(id)sender{

    if(miControl.selectedSegmentIndex == 0)
    {
        //[mapView_ clear];

        [self removeAllPinsButUserLocation2];
        NSLog(@"********0");
        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 1){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"id"];

                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];

                annotation3.grupo = 1;
                [self.mapView addAnnotation:annotation3];

            }
        }
    }
    else if(miControl.selectedSegmentIndex == 1)
    {
        [self removeAllPinsButUserLocation2];

        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 2){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];


                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];

                annotation3.grupo = 2;
                [self.mapView addAnnotation:annotation3];


            }
        }

        //action for the second button
    }
    else if(miControl.selectedSegmentIndex == 2)
    {
        [self removeAllPinsButUserLocation2];

        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 3){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];


                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];
                annotation3.grupo = 3;
                [self.mapView addAnnotation:annotation3];


            }
        }
    }


}

当用户从segmentedIndex控件中选择选项#2(Eventos)时,引脚为紫色,如预期的那样。然后,如果用户选择选项#1(Cursos),则引脚为绿色,如预期的那样,如果用户选择选项#0(Ofertas),则引脚为红色,如预期的那样,但之后,如果用户选择任何选项,所有引脚都是红色的。为什么???

1 个答案:

答案 0 :(得分:2)

听起来引脚视图正在从之前的注释中重复使用,并且它们pinColor在重新使用时未被更新。

在您链接的答案中,有以下声明:

  

最后,在viewForAnnotation中,在返回视图之前,根据grupo设置pinColor:

如果要重新使用出列视图,则需要设置pinColor是否要创建新视图。出列的视图可能已为其他组中的注释设置了pinColor

问题中显示的代码仅在创建新视图时设置pinColor

需要将该代码移出该块并放在return annotationView;行之前:

annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

//the code to set the pinColor goes HERE...
myAnnotation *myAnn = (myAnnotation *)annotation;
NSLog(@"ANNOTATION GRUPO = %d",myAnn.grupo);
switch (myAnn.grupo)
{
    case 1:
        annotationView.pinColor = MKPinAnnotationColorRed;
        break;
    case 2:
        annotationView.pinColor = MKPinAnnotationColorGreen;
        break;
    case 3:
        annotationView.pinColor = MKPinAnnotationColorPurple;
        break;
    default:
        annotationView.pinColor = MKPinAnnotationColorPurple;
        break;
}

return annotationView;