我在Apple的Location and Maps Programming Guide中看到了这段代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
// If the annotation is the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MyCustomAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView
dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomPinAnnotationView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
// If appropriate, customize the callout by adding accessory views (code not shown).
}
else
pinView.annotation = annotation;
return pinView;
}
return nil;
}
我对这部分有疑问:
else
pinView.annotation = annotation;
我在Stackoverflow answer中看到了相同的代码:
不相关,但您还需要设置视图的
中添加annotation
属性 当它被重新使用时(在它之后不是零的情况下) 出队)。因此,在else
:if (pinAnnotation == nil)
块else { //annotation view being re-used, set annotation to current... pinAnnotation.annotation = annotation; }
为什么在else块中设置了注释属性?无论是否重复使用视图,都不应设置注释?
答案 0 :(得分:0)
您使用注释初始化MKPinAnnotationView
,以便其他只是因此您不会将其设置两次。将它设置两次完全没问题但当然效率低下。