【问题标题】:MKAnnotations not reloading correctlyMKAnnotations 未正确重新加载
【发布时间】:2012-05-21 21:03:24
【问题描述】:

MKAnnotation 有问题。有一个自定义的 pinview 并且在第一次加载时工作正常。去移除别针,然后重新加载它们改变颜色的相同别针。我添加了来自两个不同数据库的引脚并且工作得很好。一旦删除然后分别添加每个数组,第二个数组将使用第一个数组自定义引脚而不是分配的那个。

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

已经搜索了所有内容,但似乎无法找到一个可以工作的示例或一个问题所在的想法。感谢您的帮助。

【问题讨论】:

  • 当然,我一发布就知道了。 MyAnnotation *myAnnot = (MyAnnotation *)annotation; if (myAnnot.mappin == @"42") pinView.image = [UIImage imageNamed:@"stores.png"];否则 pinView.image = [UIImage imageNamed:@"shops.png"];

标签: ios ipad mkmapview mkannotation


【解决方案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
}

您需要将设置图像的代码从“配置一次”部分中取出,并将其放入“为每个视图配置”部分。否则,每次您调用出列可重用视图时,您可能会得到配置不正确的内容。

【讨论】:

    【解决方案2】:

    这样解决了

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

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多