什么时候在 viewForAnnotation 方法中 link 注解 属性
When to link annotation property in viewForAnnotation method
我在 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;
我在 Whosebug 中看到了相同的代码 answer:
Unrelated, but you also need to set the view's annotation
property
when it is being re-used (in the case when it is not nil after the
dequeue). So add an else
block to the if (pinAnnotation == nil)
:
else {
//annotation view being re-used, set annotation to current...
pinAnnotation.annotation = annotation;
}
为什么在else块中设置注解属性?无论是否重用视图,是否都不应设置注释?
你用注解初始化 MKPinAnnotationView
所以 else 只是为了你不会设置它两次。设置两次完全没问题,当然效率低下。
我在 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;
我在 Whosebug 中看到了相同的代码 answer:
Unrelated, but you also need to set the view's
annotation
property when it is being re-used (in the case when it is not nil after the dequeue). So add anelse
block to theif (pinAnnotation == nil)
:else { //annotation view being re-used, set annotation to current... pinAnnotation.annotation = annotation; }
为什么在else块中设置注解属性?无论是否重用视图,是否都不应设置注释?
你用注解初始化 MKPinAnnotationView
所以 else 只是为了你不会设置它两次。设置两次完全没问题,当然效率低下。