这听起来像是注释视图重用问题。
当重新显示注释时,它们会重新使用具有先前注释的图像的视图。这image
视图中的属性没有被更新,因为当它被重新用于另一个注释时,它应该被更新。
In the viewForAnnotation
委托方法,这段代码看起来错误:
MKAnnotationView *annotationView = [mapView dequeue...
if (annotationView == nil)
annotationView = myLocation.annotationView;
else
annotationView.annotation = annotation;
If the dequeue
返回一个视图(即先前创建的视图,可能是为注释而创建的)不同的类型),其annotation
属性已更新,但其image
属性未更新。
现有代码仅设置image
创建新注释视图时的属性(当dequeue
回报nil
).
现在,注释view创造和image
-设置代码在注释中model class IGAMapAnnotation
。最好创建一个自定义的MKAnnotationView
自动更新的类image
财产无论何时annotation
属性已更新。
然而,另一种选择是将所有逻辑放在viewForAnnotation
委托方法本身(并删除annotationView
方法从IGAMapAnnotation
class).
更新后的示例viewForAnnotation
委托方法:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (! [annotation isKindOfClass:[IGAMapAnnotation class]])
{
//return default view if annotation is NOT of type IGAMapAnnotation...
return nil;
}
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"IGAMapAnnotation"];
//these properties don't change per annotation
//so they can be set only when creating a new view...
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
annotationView.annotation = annotation;
}
//whether we are using a completely new view or a re-used view,
//set the view's image based on the current annotation...
IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
if ([myLocation.type isEqual: @"A"] || [myLocation.type isEqual: @"B"] || [myLocation.type isEqual: @"C"])
{
annotationView.image = [UIImage imageNamed:@"circle.png"];
}
else if ([myLocation.type isEqual: @"D"])
{
annotationView.image = [UIImage imageNamed:@"triangle.png"];
}
else if ([myLocation.type isEqual: @"E"])
{
annotationView.image = [UIImage imageNamed:@"square.png"];
}
else
{
annotationView.image = [UIImage imageNamed:@"oval.png"];
}
return annotationView;
}