4

次の画像に示すように、注釈のタイトルを表示するのに問題があります。最初の画像は値を非常によく示しています。一方、値が 3 桁になると、2 番目の画像に示すように、タイトルに 3 つのドットが表示されます。この問題を解決する方法を知りたいです。どんなアイデアでも大歓迎です!よろしくお願いします。参照用にコードをここに配置しました!

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
   MKAnnotationView *pinView=nil;
    if(![annotation isKindOfClass:[Annotation class]]) // Don't mess user location
        return nil;

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

    if ([annotation isKindOfClass:[Annotation class]]) {
        Annotation *a = (Annotation *)annotation;

        pinView.image = [ZSPinAnnotation pinAnnotationWithColor:a.color];
        pinView.annotation = a;
        pinView.enabled = YES;
        pinView.centerOffset=CGPointMake(6.5,-16);
        pinView.calloutOffset = CGPointMake(-11,0);

    }

    pinView.canShowCallout = YES;
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [pinView setRightCalloutAccessoryView:rightButton];
    pinView.leftCalloutAccessoryView = [[UIView alloc] init];
    pinView.leftCalloutAccessoryView=nil;

    return pinView;
  }

私の showCallout タイトルは、次のコードで更新されます。

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];

ここに画像の説明を入力 ここに画像の説明を入力

4

2 に答える 2

2

問題は、タイトルが変更されたときに吹き出しビューのレイアウトが再計算されないことです。そのためのバグレポートを提出する必要があるかもしれません。

再レイアウトを強制するには、コールアウトが表示されている場合、プログラムで注釈を選択解除して選択できます。アニメーションではありませんが、吹き出しの幅を変更します。

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];


if ([self.mapView viewForAnnotation:annotation] != nil) {
    NSArray *selectedAnnotations = self.mapView.selectedAnnotations;
    if ((selectedAnnotations != nil) && ([self.mapView.selectedAnnotations indexOfObject:annotation] != NSNotFound)) {
        [self.mapView deselectAnnotation:annotation animated:NO];
        [self.mapView selectAnnotation:annotation animated:NO];
    }
}

いずれにせよ、注釈のタイトルは注釈のテキスト ラベルとして使用されるため、この行も削除する必要があります。

[rightButton setTitle:annotation.title forState:UIControlStateNormal];

アップデート:

@detunized が提供するソリューションは、アニメーションで機能します。ただし、不要な UIView を作成する代わりに、ボタン ビューを削除して再度追加することをお勧めします。

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];

MKAnnotationView *annotationView = [self.mapView viewForAnnotation:annotation];
// The annotation must be visible, otherwise a refresh isn't needed
if (annotationView != nil) {
    NSArray *selectedAnnotations = self.mapView.selectedAnnotations;
    // The annotation must be selected, otherwise a refresh isn't needed
    if ((selectedAnnotations != nil) && ([self.mapView.selectedAnnotations indexOfObject:annotation] != NSNotFound)) {
            // Unset and re-set the right button to force relayout
            UIView *view = annotationView.rightCalloutAccessoryView;
            annotationView.rightCalloutAccessoryView = nil;
            annotationView.rightCalloutAccessoryView = view;
    }
}
于 2012-11-12T17:09:09.243 に答える
0

この問題の正しい解決策はわかりませんが、ハッキーな解決策があります。ビューの再レイアウトをトリガーする必要があり、それはアクセサリ ビューをいじることで実現できます。ここでは左アクセサリ ビューを使用していないので、何かに設定してから に戻すことができますnil。このような:

self.pin.title = @"Ozone Level: 100";
self.pin_view.leftCalloutAccessoryView = [[[UIView alloc] init] autorelease];
self.pin_view.leftCalloutAccessoryView = nil;

試してみたところ、iOS 5 で動作します。iOS 6 ではテストできません。

それを頻繁に行う場合は、UIView常に作成するのではなく、何かを再利用することをお勧めします。

更新中に同じ問題が発生しましたrightCalloutAccessoryViewnil最初に設定し、すぐに最初に必要なものに設定した場合にのみ機能しました。

于 2012-11-08T00:10:21.103 に答える