我有一个装载了大量自定义注释的MKMapView (800个左右)。
当我拖动地图并返回到一个注释时,它的图像与另一个注释发生了变化。对我来说,这似乎是一个缓存问题。
拖曳前销

拖曳后销

SuperClass报头
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation>
@property (nonatomic, copy) NSString *title;
@property NSString * pinImageName;
@property (nonatomic) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title pinImageName:(NSString *)pinImageName;
- (MKAnnotationView *)getAnnotationView;
@endSubClass (创建问题的那个)标头
#import "MapAnnotation.h"
#import "Company.h"
@interface CompanyAnnotation : MapAnnotation
@property Company *company;
@property UIImage *pinImage;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title pinImage:(UIImage *)pinImage;
- (MKAnnotationView *)getAnnotationView;
@endviewForAnnotation委托方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if (annotation == mapView.userLocation){
return nil;
}
MapAnnotation *location = [MapAnnotation new];
MKAnnotationView *annotationView = [MKAnnotationView new];
if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
location = (CompanyAnnotation *)annotation;
annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"companyAnnotation"];
}
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}getAnnotationView法
- (MKAnnotationView *)getAnnotationView {
MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"companyAnnotation"];
[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]];
return annotationView;
}发布于 2015-05-09 15:27:38
在viewForAnnotation中没有正确处理去队列,因为当dequeueReusableAnnotationViewWithIdentifier返回以前使用过的视图(当annotationView不是nil)时,代码只更新该视图的annotation属性:
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
}但是注释视图的image没有更新--在脱队列视图中,image将被设置为与视图最初创建的注释相关联的注释(当getAnnotationView被调用时)。
因此,现在视图出现在新注释的坐标处,但图像仍然来自视图所使用的前一个注释。
修复这个问题有多种方法,例如创建一个适当的MKAnnotationView子类,它监视对其annotation属性的更改,并自动更新与注释相关的所有其他属性。
使用现有代码,修复它的一个简单方法是将特定于注释的属性分离为一个单独的方法,在创建视图和更新其annotation属性时都可以调用该方法。
例如,在CompanyAnnotation中,创建如下方法:
-(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;
}最后在viewForAnnotation中
if (annotationView == nil) {
annotationView = [location getAnnotationView];
} else {
annotationView.annotation = annotation;
if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
//update image, etc...
[annotation configureView:annotationView];
}
}请注意,MapAnnotation将在pinImageName属性中遇到同样的问题。
https://stackoverflow.com/questions/30122625
复制相似问题