根据注释的属性设置注释的字形文本

Set annotation's glyphtext based on the annotation's properties

我 运行 遇到自定义注释显示不正确的问题。在我的代码中,我检查当前注释是否针对具有给定唯一标识符的站。如果是这样,我自定义它的属性。

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 重用了注释。我怎样才能防止这种情况发生?

正如您所说,这是由于AnnotationView 重用了注解。试试下面的代码:

    if (station.id == "26") {
            glyphText = "p"
            markerTintColor = UIColor(named: "Blue")
    } else {
            glyphText = // Default text
            markerTintColor = // Default color
    }