我遇到了自定义注释显示不正确的问题。在我的代码中,我检查当前注释是否针对具有给定唯一标识符的电台。如果是这样,我可以自定义其属性。
StationAnnotationView.swift
class StationAnnotationView: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let station = newValue as? Station else { return }
clusteringIdentifier = nil
displayPriority = .required
if (station.id == "26") {
glyphText = "p"
markerTintColor = UIColor(named: "Blue")
}
}
}
首先,我的mapView
正确显示注释(即,使用station.id == 26
更改唯一电台的颜色和字形),但是在平移和缩放一段时间后,我的自定义格式开始应用于其他注释(这不会发生,因为任何给定的station.id
只有一个工作站)。我怀疑是由于AnnotationView
重用了注释。如何防止这种情况发生?
答案 0 :(得分:1)
正如您所说,这是由于AnnotationView重用了注释。尝试以下代码:
if (station.id == "26") {
glyphText = "p"
markerTintColor = UIColor(named: "Blue")
} else {
glyphText = // Default text
markerTintColor = // Default color
}