本文介绍了是否有任何方法可以重置dequeueReusableAnnotationViewWithIdentifier中使用的队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图视图中添加了自定义注释.根据应用程序的内部模型,注释可以更改颜色和标题(尽管位置可能不会更改).我在方法

I add custom annotation in a mapview. Based on the internal model of the app the annotations can change color and title (although position may not change). I am using dequeueReusableAnnotationViewWithIdentifierin the method

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

奇怪的是,当模型更改时,注释会刷新"以使用正确的标题和颜色(我只是使用removeAnnotations:并添加新的标题和颜色),但是稍后在使用地图时会使用一些旧注释错误的颜色出队.每次模型更改时,我都会使用一个不同的标识符.

The strange thing is that when the model changes the annotations are "refreshed" to use the correct title and color (I just use removeAnnotations: and add the new ones), but later when playing with the map some old annotations with wrong color are dequeued. I use a different identifier each time the model changes.

下面是代码:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{
    // in case it's the user location, we already have an annotation, so just return nil
    if ([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
    }
    if ([annotation isKindOfClass:[APGSAnnotation class]]){
        APGSAnnotation *gsn = (APGSAnnotation*) annotation;
        NSString *GSAnnotationIdentifier = [NSString stringWithFormat:@"gid_%lu_%@", (unsigned long)gsn.gs.gID, self.car.energyType];

        MKAnnotationView *markerView = [theMapView dequeueReusableAnnotationViewWithIdentifier:GSAnnotationIdentifier];
        if (markerView == nil) {
            MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation                                                                            reuseIdentifier:GSAnnotationIdentifier];
            annotationView.canShowCallout = YES;
            annotationView.image = [self customizeAnnotationImage:gsn.gs];
            annotationView.opaque = NO;

            UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:gsn.logo]];
            annotationView.leftCalloutAccessoryView = sfIconView;

            // http://stackoverflow.com/questions/8165262/mkannotation-image-offset-with-custom-pin-image
            annotationView.centerOffset = CGPointMake(0,-annotationView.image.size.height/2);


            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
            annotationView.rightCalloutAccessoryView = rightButton;

            return annotationView;
        }else{
            markerView.annotation = annotation;
            return markerView;
        }
    }
    return nil;
}

和自定义方法

- (UIImage*)customizeAnnotationImage:(APGS*)gs{
    UIImage *markerImage;

    if (gs.gID == self.best.gID) {
        markerImage = [UIImage imageNamed:@"marker_red.png"];
    }else if (gs.type == k1){
        markerImage = [UIImage imageNamed:@"marker_blue.png"];
    }else if (gs.type == k2){
        markerImage = [UIImage imageNamed:@"marker_green.png"];
    }else if (gs.type == k3){
        markerImage = [UIImage imageNamed:@"marker_purple.png"];
    }else if (gs.type == k4){
        markerImage = [UIImage imageNamed:@"marker_brown.png"];
    }
    UIImage *logoImage = [UIImage imageNamed:gs.logo];
    // size the flag down to the appropriate size
    CGRect resizeRect;
    resizeRect.size = markerImage.size;
    CGSize maxSize = CGRectInset(self.view.bounds, kAnnotationPadding, kAnnotationPadding).size;

    maxSize.height -= self.navigationController.navigationBar.frame.size.height + kCallOutHeight;

    if (resizeRect.size.width > maxSize.width)
        resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);

    if (resizeRect.size.height > maxSize.height)
        resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

    resizeRect.origin = CGPointMake(0.0, 0.0);
    float initialWidth = resizeRect.size.width;

    UIGraphicsBeginImageContextWithOptions(resizeRect.size, NO, 0.0f);
    [markerImage drawInRect:resizeRect];
    resizeRect.size.width = resizeRect.size.width/2;
    resizeRect.size.height = resizeRect.size.height/2;

    resizeRect.origin.x = resizeRect.origin.x + (initialWidth - resizeRect.size.width)/2;
    resizeRect.origin.y = resizeRect.origin.y + kLogoHeightPadding;

    [logoImage drawInRect:resizeRect];


    // Create string drawing context
    UIFont *font = [UIFont fontWithName:@"DBLCDTempBlack" size:11.2];
    NSString * num = [NSString stringWithFormat:@"%4.3f",[gs getL]];
    NSDictionary *textAttributes = @{NSFontAttributeName: font,
                                     NSForegroundColorAttributeName: [UIColor whiteColor]};

    CGSize textSize = [num sizeWithAttributes:textAttributes];

    NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];

    //adjust center
    if (resizeRect.size.width - textSize.width > 0) {
        resizeRect.origin.x += (resizeRect.size.width - textSize.width)/2;
    }else{
        resizeRect.origin.x -= (resizeRect.size.width - textSize.width)/2;
    }

    resizeRect.origin.y -= kTextPadding;
    [num drawWithRect:resizeRect
              options:NSStringDrawingUsesLineFragmentOrigin
           attributes:textAttributes
              context:drawingContext];

    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resizedImage;
}

基于汽车类型,注释应更改颜色.

Based on car type the annotations should change color.

推荐答案

根据标题,答案是否定的:)

Based on the title, the answer is NO :)

通常,您将使视图出队并重置视图的所有相关属性

Normally you would dequeue the view and reset all relevant properties of the view

您可以考虑的是:如果视图变化太大,则可以切换到其他reusingIdentifier,从而切换队列并规避"缓存的视图

What you can consider is: if the views would change too much, you could switch to a different reusingIdentifier, thereby switching the queue and 'circumventing' the cached views

这篇关于是否有任何方法可以重置dequeueReusableAnnotationViewWithIdentifier中使用的队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:14
查看更多