为什么在重用注释视图 mapkit 时我的标签文本没有改变 swift 4

Why doesn't my labels text change when reusing annotation views mapkit swift 4

我正在使用 mapkit 显示某些地方的自定义注释,在注释上方带有注释名称标签。我注意到在一些注释 recreated/reused 之后,标签中的文本与注释的名称不同。我认为当注释被重用时标题不会被改变。重复使用的注释越多,它们的标题就越错误。

知道为什么标签的标题在重复使用时与注释的标题不匹配吗?

这是我的代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

    let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
    annotationLabel.numberOfLines = 3
    annotationLabel.textAlignment = .center
    annotationLabel.font = UIFont(name: "Rockwell", size: 10)

    let strokeTextAttributes: [NSAttributedStringKey: Any] = [
        .strokeColor : UIColor.white,
        .foregroundColor : UIColor.black,
        .strokeWidth : -4,
        ]

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView?.animatesDrop = true
        annotationView?.canShowCallout = false

        annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
        annotationView?.addSubview(annotationLabel)
    } else {
        annotationView?.annotation = annotation
        annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
    }


    annotationLabel.clipsToBounds = true
    annotationLabel.backgroundColor = UIColor.white
    annotationLabel.layer.cornerRadius = 15

    return annotationView
}

因此,无论哪种方式,您都可以看到标签的文本应更改为标签的标题,因为无论是否已创建,我都将文本更改为当前注释。

这里有一些图片可以让你更好地理解:

如您所见,"Luna Blu" 是 Tiburon 的一家餐馆。在需要更多注释并重复使用该注释后,它表示 "Luna Blu" 在城市中间。为了表明这实际上是不正确的,当我单击注释时,会出现一个带有真正注释标题的滑出菜单,即 "Taylor Street Coffee Shop".

当注释不为 nil 并且控制转到 else 时,标签的标题会更改,但它不是添加到 annotationView 的标签,它是将在 func returns 时释放的局部变量

annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)

因此您必须在视图中查询该标签,可能是通过您在添加和分配文本之前给它的标签

//

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

    let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
    annotationLabel.numberOfLines = 3
    annotationLabel.textAlignment = .center
    annotationLabel.font = UIFont(name: "Rockwell", size: 10)
    annotationLabel.tag = 22
    let strokeTextAttributes: [NSAttributedStringKey: Any] = [
        .strokeColor : UIColor.white,
        .foregroundColor : UIColor.black,
        .strokeWidth : -4,
        ]

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView?.animatesDrop = true
        annotationView?.canShowCallout = false

        annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
        annotationView?.addSubview(annotationLabel)
    } else {
        annotationView?.annotation = annotation

        for item in annotationView!.subviews {
           if item.tag == 22 {
               let lbl = item as! UILabel
               lbl.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
               break
           }
        }

    }


    annotationLabel.clipsToBounds = true
    annotationLabel.backgroundColor = UIColor.white
    annotationLabel.layer.cornerRadius = 15

    return annotationView
}