2012-05-21 81 views
0

遇到MKAnnotation问题。有一个自定义的pinview,并为第一次加载工作正常。去除引脚,然后重新加载相同的引脚,它们改变颜色。我添加来自两个不同的数据库的引脚,工作得很好。一旦删除,然后分别添加每个阵列第二个阵列采取第一个阵列自定义引脚,而不是一个分配。MKAnnotations无法正确重新加载

- (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; 
} 

已经遍地搜索,但似乎无法得到一个例子工作或一个想法,这是分解。谢谢你的帮助。

+0

作为我文章中,我想通了。 MyAnnotation * myAnnot =(MyAnnotation *)注释;如果(myAnnot.mappin == @“42”) pinView.image = [UIImage imageNamed:@“stores.png”]; else pinView.image = [UIImage imageNamed:@“shops.png”]; –

+0

来自这里http://stackoverflow.com/questions/7288757/how-do-i-load-different-custom-pins-or-identifiers-based-off-of-their-property-v –

回答

1

得到它尽快固定这样

MyAnnotation *myAnnot = (MyAnnotation *)annotation; 

if (myAnnot.mappin == @"42") 
    customPinView.image = [UIImage imageNamed:@"shops.png"]; 
else 
    customPinView.image = [UIImage imageNamed:@"stores.png"]; 
当然
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 
} 

您需要将代码设置为“配置一次”部分,并将其放入“为每个视图配置”部分。否则,每次您打电话给出可重复使用的视图时,您都可以获得配置不正确的内容。