Принудительное обновление MKMapView viewForAnnotation

Итак, у меня есть MKMapView со всеми добавленными булавками, и цвет булавки зависит от того, установлено ли значение для этой булавки. Когда я впервые загружаю приложение, вызывается viewForAnnotationи устанавливаются соответствующие цвета. Однако, когда я обновляю детали булавки (например, местоположение, название и т. д.), я также обновляю цвет булавки, чтобы обнаружить, что он не обновляется. Похоже, viewForAnnotationбольше не вызывается после первоначального добавления.

Я прочитал много подобных вопросов и могу подтвердить, что mapView.delegate = self;

Вот мой код viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapAnnotation *)annotation
{
    if([annotation class] == MKUserLocation.class)
        return nil;

    NSString *pinIdentifier = annotation.identifier; // This is a unique string for each pin and is getting populated every time!

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];

    if(annotationView == nil)
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
    else
        annotationView.annotation = annotation; // Never had this line fire...

    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = NO;
    annotationView.enabled = YES;
    annotationView.tag = annotation.counter;

    if(annotation.pinColour == Stopped) // from enum
        annotationView.pinColor = MKPinAnnotationColorRed;
    else
        annotationView.pinColor = MKPinAnnotationColorGreen;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(mapCalloutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    infoButton.tag = annotation.counter;
    annotationView.rightCalloutAccessoryView = infoButton;

    return annotationView;
}

Вот код, где я добавляю пин:

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;

MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoord;
annotation.identifier = theIdentifier;
annotation.title = theTitle;
annotation.subtitle = theSubtitle
annotation.pinColour = [self getPinColour];
annotation.counter = theCounter;

[theMapView addAnnotation:annotation];

Вот код, где я обновляю пин (другой метод добавления):

updatePin = true;
pinCounter = mapPin.counter;

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;
[mapPin setCoordinate:annotationCoord];

mapPin.identifier = theIdentifier;
mapPin.subtitle = theSubtitle;
mapPin.pinColour = [self getPinColour];

Я не уверен, что мне не хватает. viewForAnnotationявно работает, просто он никогда не вызывается после первоначального добавления! Если бы эта функция была вызвана, я на 100 % уверен, что она сработает, поскольку она меняет цвет, если я перезапускаю приложение!

РЕДАКТИРОВАТЬ: О, и я действительно не хочу начинать удалять аннотации и добавлять их заново. Это то, что я делаю в краткосрочной перспективе в любом случае!

16
задан Juan Boero 4 February 2016 в 20:06
поделиться