根据所选的MKAnnotationViewdynamic更改leftCalloutAccessoryView

我有一个图像arrays,与我的地图上的每个Annotation相关联。 我可以静态添加一个图像到leftCalloutAccessoryView但我不确定如何使这个dynamic。 我希望它清楚我所要求的。 每个注释都有我自己想要显示的独立图像,但是我不确定如何用下面的方法引用图像。

 - (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation { if([annotation isKindOfClass:[MKUserLocation class]]) return nil; NSString *annotationIdentifier = @"PinViewAnnotation"; MyAnnotationView *pinView = (MyAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; if (!pinView) { pinView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; pinView.canShowCallout = YES; UIImageView *houseIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon"]];//static image [houseIconView setFrame:CGRectMake(0, 0, 30, 30)]; pinView.leftCalloutAccessoryView = houseIconView; } else { pinView.annotation = annotation; } return pinView; } 

我的数组“self.sandwiches”包含具有名称( NSString )和imageName('NSString')的Sandwich对象。

我寻找一个解决scheme,我可以得到所select的引脚的索引,类似于你可以得到其索引的UITableView ,并使用indexPath.row从数组中访问它。

我的注释类; .h #import #import #import

 @interface SandwichAnnotation : NSObject<MKAnnotation> @property (nonatomic,assign) CLLocationCoordinate2D coordinate; @property (nonatomic,copy) NSString * title; @property (nonatomic,copy) NSString * subtitle; @end 

.M

 #import "SandwichAnnotation.h" @implementation SandwichAnnotation @synthesize coordinate,title,subtitle; @end 

viewForAnnotation ,而不是“获取引脚的索引”(这将工作,但效率不如在UITableView ),我build议添加所需的数据注释类本身。

这样,数据是更独立的,委托方法或其他地方的代码不需要担心,知道或与保存注释对象的位置或结构types保持同步。只要你有一个对注释对象的引用,你将立即拥有该注释所需的所有数据(或者至less它将包含对其内部相关数据的引用)。

viewForAnnotation委托方法提供了它所需要的注解对象( annotation参数)的引用。 它的一般types是id<MKAnnotation>但它实际上是创build的确切types的一个实例(可以是你的SandwichAnnotation ,也可以是Map的视图的MKUserLocation )。

一个select是让父Sandwich类本身实现MKAnnotation并消除SandwichAnnotation类。 这样,根本不需要search或引用,因为annotation参数实际上 Sandwich

但是,您可能想要为注释对象保留一个单独的类(这很好)。 在这种情况下,您可以在注释类中添加对父对象的引用。 例:

 @interface SandwichAnnotation : NSObject<MKAnnotation> @property (nonatomic,assign) CLLocationCoordinate2D coordinate; @property (nonatomic,copy) NSString * title; @property (nonatomic,copy) NSString * subtitle; @property (nonatomic,retain) Sandwich * whichSandwich; // <-- add reference @end 

在创buildSandwichAnnotation ,设置注释所针对的Sandwich的引用:

 for (Sandwich *currentSandwich in self.sandwiches) { SandwichAnnotation *sa = [[SandwichAnnotation alloc] init...]; sa.coordinate = ... sa.title = ... sa.whichSandwich = currentSandwich; // <-- set reference [mapView addAnnotation:sa]; } 

最后,在viewForAnnotation ,如果annotation的types是SandwichAnnotation ,则设置leftCalloutAccessoryView

 - (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation { if (! [annotation isKindOfClass:[SandwichAnnotation class]]) { //If annotation is not a SandwichAnnotation, return default view... //This includes MKUserLocation. return nil; } //At this point, we know annotation is of type SandwichAnnotation. //Cast it to that type so we can get at the custom properties. SandwichAnnotation *sa = (SandwichAnnotation *)annotation; NSString *annotationIdentifier = @"PinViewAnnotation"; MyAnnotationView *pinView = (MyAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; if (!pinView) { pinView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; pinView.canShowCallout = YES; //Here, just initialize a blank UIImageView ready to use. //Set image below AFTER we have a dequeued or new view ready. UIImageView *houseIconView = [[UIImageView alloc] init]; [houseIconView setFrame:CGRectMake(0, 0, 30, 30)]; pinView.leftCalloutAccessoryView = houseIconView; } else { pinView.annotation = annotation; } //At this point, we have a dequeued or new view ready to use //and pointing to the correct annotation. //Update image on the leftCalloutAccessoryView here //(not just when creating the view otherwise an annotation //that gets a dequeued view will show an image of another annotation). UIImageView *houseIconView = (UIImageView *)pinView.leftCalloutAccessoryView; NSString *saImageName = sa.whichSandwich.imageName; UIImage *houseIcon = [UIImage imageNamed: saImageName]; if (houseIcon == nil) { //In case the image was not found, //set houseIcon to some default image. houseIcon = someDefaultImage; } houseIconView.image = houseIcon; return pinView; }