swift - 重新注册一个 AnnotationView
问题描述
A
并且B
是不同种类的MKMarkerAnnotationView
类。在我的 MapViewController 我A
这样注册:
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(A.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
for annotation in annotations {
mapView.addAnnotation(annotation)
}
}
我有一个UISwitch
我想用来将注册的 AnnotationView 从更改A
为B
.
@IBAction func didToggleSwitch(_ sender: Any) {
mapView.register(B.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
// remove all annotations
mapView.removeAnnotations(mapView.annotations)
//populate all annotations
for annotation in annotations {
mapView.addAnnotation(annotation)
}
}
注册的视图没有被更新。任何想法为什么?
编辑:我注意到,当我缩小并平移地图时,一些注释会更新到新的 AnnotationView,而有些则不会。我怀疑这是由于使用了reuseIdentifier
. 如何让所有注释符合新的 AnnotationView?
解决方案
我想到了。我在我的类中创建了一个布尔值,并基于该布尔值MKAnnotation
在 annotationView 中配置了注释属性。A
当我点击 时UISwitch
,我会遍历所有注释对象,切换它们的布尔值,然后重新注册 annotationView,然后加载它们。
推荐阅读
- c# - 如何在 C#(Aspose)中设置 PPT 中的图像位置和图像大小?
- python - 基于计数器在Django中动态创建新URL
- python-3.x - 安装构建依赖项时出现spacy安装错误
- c# - 在 C# .net 或 .netcore 中以编程方式比较 sql 数据库模式的方法
- r - 如何使用基本 R 函数对多个变量的数据框进行排序,这些变量的名称在向量中给出?
- google-sheets - 将合并的 Google 表格拆分为每人单独的表格
- netty - 如何在整个“会话”中保持 Netty NIODatagram 通道打开
- typescript - Vue.js 自定义指令使用 TypeScript 检测点击外部元素
- html - Htaccess 语言重写规则给出错误 500
- sql-server - 从项目文件夹中获取 database.mdf 的直接路径 - 文件已移动