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

これによりエラーは発生しません(すべてを正しく貼り付けて変換したと仮定します)が、カスタム MKAnnotationView と MKAnnotation の両方を含む必要がある NSMutableDictionary ですが、リクエストが完了する前にそれらをすべてログに記録し、リクエストが完了すると正しく表示されます。 MKAnnotationView と MKAnnotation の両方が私が期待したものではないかのように、注釈を変更して画像を変更したり、注釈ビューを更新したりすることはできません。

どんなアイデアでも大歓迎です!

4

1 に答える 1

6

マーカー配列から nil 値を取得している理由がわかりません (特に注釈の場合)。ただし、そのような注釈ビューへの参照を保存することはお勧めしません。

デリゲート メソッドはviewForAnnotation、マップ ビューが必要と判断したときにいつでも呼び出すことができ、ビュー オブジェクトは呼び出しごとに変更される可能性があります。注釈ごとに同じ再利用識別子を使用して注釈ビューも再利用しているため、同じビュー オブジェクトが後で別の注釈に再利用される可能性もあります。

代わりに、updateStation次のことをお勧めします。

  • マップ ビューのannotations配列をループする
  • 注釈のタイプが の場合は、更新されているものと一致するEstacionEBかどうかを確認しますeid
  • 注釈subTitleelStatusプロパティを更新します (画像を設定するためにデリゲート メソッドelStatusによって使用されるため、更新することが重要です)viewForAnnotation
  • マップ ビューのviewForAnnotation:インスタンス メソッドを呼び出して、注釈の現在のビューを取得します (これはデリゲート メソッドとは異なります)mapView:viewForAnnotation:
  • imageビューのプロパティを更新します

同様の例については、この他の関連する質問を参照してください。

于 2011-07-03T15:03:28.470 に答える