我有一组图像,这些图像与 map 上的每个Annotation
相关联。我可以将图片静态添加到leftCalloutAccessoryView
中,但不确定如何使它动态化。我希望清楚我的要求。每个注释都有自己想要显示的图像,但是我不确定如何通过以下方法引用该图像;
- (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”包含具有名称(
Sandwich
)和imageName('NSString')的NSString
对象。我正在寻找一种解决方案,可以获取所选引脚的索引,类似于
UITableView
,您可以获取其索引,并使用indexPath.row
从数组访问它。我的注释课;
。H
#进口
#进口
#进口
@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
)。
这样,数据将更加独立,并且无需担心,知道或与注释对象存储在何处或哪种结构保持同步,委托方法或其他地方的代码。您具有对注释对象的引用,您将立即拥有该注释所需的所有数据(或者至少它将在其内部包含对相关数据的引用)。viewForAnnotation
委托方法提供对其需要查看的注释对象的引用(annotation
参数)。通常将其键入为id<MKAnnotation>
,但实际上它是所创建的确切类型的实例(您自己创建的SandwichAnnotation
或 map 视图中的MKUserLocation
)。
一种选择是使父Sandwich
类本身实现MKAnnotation
并消除SandwichAnnotation
类。这样,由于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
创建
SandwichAnnotation
时,设置注释用于的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
类型为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;
}
关于ios - 根据所选的MKAnnotationView动态更改leftCalloutAccessoryView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25248405/