MKAnnotation坚持使用旧信息

时间:2014-09-29 12:03:58

标签: ios core-location mkannotation mkannotationview

我正在开发一个应用程序,其中农民在他的作物中的兴趣点丢弃自定义注释(带数字的针脚),通过电子邮件将带有屏幕截图的报告发送回他的办公室,然后移动到他的下一个围场。

part screenshot of field of pins

发送电子邮件的部分代码包括重置属性和清零数组和ivars等。他的第一个字段工作正常,但后面所有字段中POI的编号都是乱七八糟的。由错误引脚表示的数据是正确的,只是引脚本身不是(更多的是在瞬间>> **)。

所以我已经确定导致引脚掉落的任何内容没有任何问题,我已经通过以下方式清除了我的注释:

[ self.mapView removeAnnotations:self.mapView.annotations];

Apple文档提到还需要在重置时实现此方法:

[ self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"]; 

因为即使他们已从屏幕上清除,他们仍然存在于内存中。然而,这仍然无法解决我的问题。如果我用以下方式明确擦除我的注释:

self.mapView.annotations = nil;

问题仍然存在。注释上的数字在第2和第3个字段中随机出现。通过登录到屏幕上的textView,我可以看到数组中包含正确的POI编号值。关于CLLocation或MKAnnotation的东西仍然存在于某个地方。 Apple的Class Reference中没有关于如何重置CLLocationManager的内容,因此我认为它是通过简单的Start和Stop完成的。我对它的使用是正确平衡的,所以我没有运行多个实例。

**指与GT;这是决定放下什么引脚的片段,动作发生在评论的中间

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

    static NSString *identifier = @"myAnnotation";
MKAnnotationView * annotationView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

if (!annotationView)
{
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];


            // standard stuff up to here
            // findings build up as a score of each test point.

    if (contentScore == 0)
    {
            // a green asterisk marker if no score was achieved

    annotationView.image = [UIImage imageNamed:@"markerZero.png"];    
    } else {

            // the marker number comes from idx in SiteCount Array

    str = [NSString stringWithFormat:@"marker%d.png",siteCount[idx];  
    annotationView.image = [UIImage imageNamed:str];
    }


} else {
  annotationView.annotation = annotation;
}

return annotationView;
}

目前,解决方法是在主屏幕上取消内存并在开始下一个字段之前重新启动的繁琐工作。我可以回去使用系统提供的红色和绿色针脚,但他已被数字交叉引用报告所破坏。

那么我应该在哪里看?谁是罪魁祸首?我怀疑MKAnnonation,但我的知识已经用完了

1 个答案:

答案 0 :(得分:0)

正如@Anna所说,如果可重用的视图已经出列,那么你并没有完全重新初始化你的注释视图 -

你应该有像

这样的东西
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *identifier = @"myAnnotation";

    MKAnnotationView * annotationView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    }                       //Note this closing brace!

            // standard stuff up to here
            // findings build up as a score of each test point.

    if (contentScore == 0) {
            // a green asterisk marker if no score was achieved
        annotationView.image = [UIImage imageNamed:@"markerZero.png"];    
    } else {
            // the marker number comes from idx in SiteCount Array
        str = [NSString stringWithFormat:@"marker%d.png",siteCount[idx];  // As per @anna's comment I am not sure how you are managing this -  It would be better if the number came from the associated annotation object  
        annotationView.image = [UIImage imageNamed:str];
    }

    annotationView.annotation = annotation;
}