【发布时间】:2016-02-10 15:19:38
【问题描述】:
我在 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属性 当它被重新使用时(在它不是 nil 的情况下 出队)。所以在if (pinAnnotation == nil)中添加一个else块:else { //annotation view being re-used, set annotation to current... pinAnnotation.annotation = annotation; }
为什么要在else块中设置注解属性?无论视图是否被重用,不应该设置注释吗?
【问题讨论】:
标签: ios mapkit mapkitannotation