Ios MKAnnotationView图像更改问题
我有一个MKMapView,里面有大量的自定义注释(大约800个) 当我在地图上拖动并返回到注释时,它的图像已被另一个注释更改。对我来说,这似乎是一个缓存问题 拖动前先锁定 拖后销 超类标题 viewForAnnotation委托方法 getAnnotationView方法Ios MKAnnotationView图像更改问题,ios,mkmapview,mkannotation,mkannotationview,Ios,Mkmapview,Mkannotation,Mkannotationview,我有一个MKMapView,里面有大量的自定义注释(大约800个) 当我在地图上拖动并返回到注释时,它的图像已被另一个注释更改。对我来说,这似乎是一个缓存问题 拖动前先锁定 拖后销 超类标题 viewForAnnotation委托方法 getAnnotationView方法 在注释视图中未正确处理出列,因为当dequeueReusableAnnotationViewWithIdentifier返回以前使用的视图时(当annotationView不是nil时),代码仅更新该视图的注释属性: i
在
注释视图
中未正确处理出列,因为当dequeueReusableAnnotationViewWithIdentifier
返回以前使用的视图时(当annotationView
不是nil
时),代码仅更新该视图的注释
属性:
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
}
但是注释视图的图像
不会更新——在出列视图中,图像
将被设置为与最初为其创建视图的注释关联的图像(调用getAnnotationView
时)
因此,现在视图显示在新注释的坐标处,但图像仍然来自视图用于的上一个注释
有多种方法可以解决此问题,例如创建
MKAnnotationView
的适当子类,该子类监视对其annotation
属性的更改,并自动更新与批注关联的所有其他属性
对于现有代码,一种简单的修复方法是将注释特定的属性更改分离为一个单独的方法,该方法可以在创建视图和更新其注释属性时调用
例如,在companynotation
中,创建如下方法:
-(void)configureView:(MKAnnotationView *)av
{
av.image = self.pinImage;
}
然后将getAnnotationView
更改为:
- (MKAnnotationView *)getAnnotationView {
MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"companyAnnotation"];
//set properties here that are not specific to an annotation...
[annotationView setEnabled:YES];
[annotationView setCanShowCallout:YES];
[annotationView setContentMode:UIViewContentModeScaleAspectFit];
//[annotationView setImage:self.pinImage];
[annotationView setFrame:CGRectMake(0, 0, 28, 44)];
[annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
//set properties that are specific to an annotation...
[self configureView:annotationView];
return annotationView;
}
最后在注释的视图中:
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
//update image, etc...
[annotation configureView:annotationView];
}
}
请注意,MapAnnotation
将与pinImageName
属性有相同的问题。能否显示viewForAnnotation
委托方法和getAnnotationView
方法的代码?听起来好像出列没有得到正确处理(如您所说的“缓存”问题)。您的问题没有提到实现的主要部分,即注释创建。请描述一下。
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
}
-(void)configureView:(MKAnnotationView *)av
{
av.image = self.pinImage;
}
- (MKAnnotationView *)getAnnotationView {
MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"companyAnnotation"];
//set properties here that are not specific to an annotation...
[annotationView setEnabled:YES];
[annotationView setCanShowCallout:YES];
[annotationView setContentMode:UIViewContentModeScaleAspectFit];
//[annotationView setImage:self.pinImage];
[annotationView setFrame:CGRectMake(0, 0, 28, 44)];
[annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
//set properties that are specific to an annotation...
[self configureView:annotationView];
return annotationView;
}
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
//update image, etc...
[annotation configureView:annotationView];
}
}