MKAnnotations未正确重新加载

时间:2012-05-21 21:03:24

标签: ios ipad mkmapview mkannotation

遇到MKAnnotation问题。有一个自定义的pinview并适用于第一次加载。去除引脚然后重新加载相同的引脚,他们改变颜色。我从两个不同的DB添加引脚,工作得很好。一旦删除然后分别添加每个数组,第二个数组将获取第一个数组自定义引脚而不是指定的数组。

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

MKAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{
    static NSString *defaultPinID = @"pin";
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) 
        pinView = [[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID];

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;

    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stores.png"]];
    pinView.leftCalloutAccessoryView = profileIconView;

    NSString *badgestringss = @"8 reviews";
    customBadge1 = [CustomBadge customBadgeWithString:badgestringss 
                                      withStringColor:[UIColor whiteColor]
                                       withInsetColor:RGB(255, 51, 0)
                                       withBadgeFrame:YES 
                                  withBadgeFrameColor:[UIColor whiteColor] 
                                            withScale:1.0
                                          withShining:YES];
    [customBadge1 setFrame:CGRectMake(100, 1, 85, 15)];
    [pinView.leftCalloutAccessoryView addSubview:customBadge1];



    if(setStoreOrShops==NO){
    pinView.image = [UIImage imageNamed:@"stores.png"];    //as suggested by Squatch  
    }
    else if (setStoreOrShops==YES){
         pinView.image = [UIImage imageNamed:@"shops.png"];   
    }


   else {
    [mapView.userLocation setTitle:@"Current Location"];
}
     }
return pinView;
}

已经搜遍了所有但似乎无法得到一个工作的例子或一个想法,这是破坏。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我不确定你改变颜色是什么意思,因为我不知道你的CustomBadge代码做什么或你的图像是什么样的。但是,问题可能是您在视图的初始设置中应用条件逻辑。之后当dequeReusableAnnotationViewWithIdentifier:实际返回一些东西时,它已被配置为其他东西。

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

    MKAnnotationView *pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"default"];
    if (pinView == nil) {
        // CONFIGURE ONCE
        // things that are set up once and never change
    }

    // CONFIGURE EVERY VIEW
    // view configurations that change every pin
}

您需要将设置图像的代码从“一次配置”部分中取出,并将其放入“为每个视图配置”部分。否则,每次打电话给可重复使用的视图出列时,都会得到错误配置的内容。

答案 1 :(得分:1)

以这种方式解决了这个问题

MyAnnotation *myAnnot = (MyAnnotation *)annotation;

if (myAnnot.mappin == @"42")
    customPinView.image = [UIImage imageNamed:@"shops.png"];
else
    customPinView.image = [UIImage imageNamed:@"stores.png"];