我被困在这里,我想我应该问是否有人可以帮助我,因为我正准备撕掉我的头发。
我正在使用下面的教程/指南并得到了最后的结果,它对于一张图像非常有效,但我想要的是能够为每个单独的注释设置不同的图像/缩略图
我已经尝试让它工作一段时间了,我已经受够了。哈哈
我对 xcode 还很陌生,所以这就是我为之苦苦挣扎的原因。
摘自OP的“对答案的建议编辑”:
编辑:
我似乎无法理解你所说的内容,我对此感到困惑。
我在下面的 Dropbox 中添加了我的项目副本(如果您可以查看)。目前可能有点困惑,因为我已经尝试了很长时间但一无所获。
https://www.dropbox.com/sh/6ddhege240konzo/XrpuFYF5y5
我很想弄清楚如何进行自定义标注。
我不确定的一件事是代表以及他们的实际工作方式。
最佳答案
您想要将每个注释的 leftCalloutAccessoryView
设置为不同的图像。
leftCalloutAccessoryView
是 MKAnnotationView
的一个属性,因此您需要在 viewForAnnotation
委托(delegate)方法中设置它(您可以在该方法中创建并返回一个 MKAnnotationView
)。
viewForAnnotation
委托(delegate)方法获取对注释的引用,您需要在 annotation
参数中为其创建 View 。
因此,根据annotation
的某些属性,您可以相应地设置leftCalloutAccessoryView
。
在最粗略的层面上,您可以根据 annotation.title
设置 leftCalloutAccessoryView
。
例如:如果标题是“SFO”,则将图像设置为“apple”,如果标题是“ATL”,则将图像设置为“peach”等。
但是,最好创建一个单独的属性(在实现 MKAnnotation
的注释类中)来清楚地指示用于注释的图像。此属性可以是 UIImage
本身、图像的名称、数字等——任何最适合您情况的属性。
创建注释时,在调用 addAnnotation
之前,您可以设置注释的此属性。
然后在 viewForAnnotation
委托(delegate)方法中,根据自定义注释属性设置 leftCalloutAccessoryView
。
例如,假设名为 imageName
的 NSString
属性已添加到注释类中:
MKAnnotationView *av = ... //or MKPinAnnotationView
//typical dequeue and alloc/init code here
if ([annotation isKindOfClass:[MyAnnotationClass class]])
{
//cast the annotation parameter to your custom class
//so you can easily access the custom properties...
MyAnnotationClass *myAnn = (MyAnnotationClass *)annotation;
//create UIImage based on custom property of annotation...
UIImage *img = [UIImage imageNamed:myAnn.imageName];
//create UIImageView to use for the leftCalloutAccessoryView...
UIImageView *iv = [[[UIImageView alloc] initWithImage:img] autorelease];
//if using ARC, remove the autorelease above
av.leftCalloutAccessoryView = iv;
}
return av;
关于ios - 如何为每个注释显示不同的左侧附件图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12377532/