2

MKAnnotationView在异步请求完成并包含有关注释状态的信息后,我无法找到更新自定义图像的方法。到目前为止,我有这个:

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

    static NSString *identifier = @"EstacionEB";   
    if ([annotation isKindOfClass:[EstacionEB class]]) {
        EstacionEB *location = (EstacionEB *) annotation;

        CustomPin *annotationView = (CustomPin *) [_mapita dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[CustomPin alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annotationView.annotation = annotation;
        }

        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [location elStatus]]];

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image = image;

        NSDictionary *temp = [[NSDictionary alloc] 
                              initWithObjects:[NSArray arrayWithObjects:annotationView, location, nil]
                              forKeys:[NSArray arrayWithObjects:@"view", @"annotation", nil]
                              ];
        //This array is synthesized and inited in my controller's viewDidLoad
        [self.markers setObject:temp forKey:location.eid];
        return annotationView;
    }

    return nil;    
}

不久之后,我做了一个请求,结果是一个 NSDictionary,我正在尝试执行以下操作,这两个元素都返回 null:

- (void)updateStation:(NSString *)eid withDetails:(NSDictionary *)details
{
    NSInteger free = [details objectForKey:@"free"];
    NSInteger parkings = [details objectForKey:@"parkings"];

    NSDictionary *storedStations = [self.markers objectForKey:eid];

    CustomPin *pin = [storedStations objectForKey:@"view"]; //nil
    EstacionEB *station = [referencia objectForKey:@"annotation"]; //nil as well

    [station setSubtitle:free];

    NSString *status;
    if( free==0 ){
        status = @"empty";
    } else if( (free.intValue>0) && (parkings.intValue<=3)  ){
        status = @"warning";
    } else {
        status = @"available";
    }
    UIImage * image = [UIImage imageNamed:[NSString imageWithFormat:@"%@.png", status]];
    pin.image = image;
}

这不会产生任何错误(假设我正确粘贴和转换了所有内容),但是 NSMutableDictionary 应该包含我的自定义 MKAnnotationView 和 MKAnnotation,但是即使我在请求完成之前将它们全部记录并且它正确显示,当请求完成时好像 MKAnnotationView 和 MKAnnotation 都不是我所期望的,因此,我无法修改注释以更改图像或更新注释视图。

任何想法,将不胜感激!

4

1 回答 1

6

我不确定您为什么从标记数组中获取 nil 值(尤其是对于注释)。但是,我不建议像这样存储对注释视图的引用。

地图视图可以在它认为有必要的viewForAnnotation任何时候调用委托方法,并且视图对象可以从一个调用更改为下一个调用。由于您还对每个注释使用相同的重用标识符来重用注释视图,因此以后也有可能将相同的视图对象重用于另一个注释。

相反,在 中updateStation,我建议如下:

  • 循环遍历地图视图的annotations数组
  • 如果注解是 type EstacionEB,则检查它是否eid与正在更新的注解匹配
  • 更新注解subTitleelStatus属性(更新很重要,elStatus因为viewForAnnotation委托方法使用它来设置图像)
  • 通过调用地图视图的viewForAnnotation:实例方法获取注解的当前视图(这委托方法不同mapView:viewForAnnotation:
  • 更新image视图的属性

有关类似示例,请参见其他相关问题。

于 2011-07-03T15:03:28.470 回答