【问题标题】:annotation images changing after reuse重复使用后更改的注释图像
【发布时间】:2017-02-02 22:20:45
【问题描述】:

我的问题和这个类似 Displaying custom multiple pins shows wrong pins for locations 但略有不同,我正在使用 Swift。即使转换后,我也无法解决我的问题。 我有注释,每个注释都有不同的图像。图像是编号的指针。每个注释的详细信息来自一个 plist。标题如“1 个停车场”、“2 个磨坊”等。我在空格前取部分以获得对应于 png 图像的数字。

加载时,所有注释都有正确的图像。如果我平移该区域并返回,图像可以相互交换。似乎是重用的问题,但我看不到解决方案。 getMapAnnotations() 在 vi​​ewdidload 中被调用。

func getMapAnnotations() -> [Stands] {
    var annotations:Array = [Stands]()
    print("ANNOTATIONS")
    //load plist file
    var stands: NSArray?
    if let path = Bundle.main.path(forResource: "stands", ofType: "plist") {
      stands = NSArray(contentsOfFile: path)
      }
    //iterate and create annotations
    if let items = stands {
      for item in items {
        let lat = (item as AnyObject).value(forKey: "lat") as! Double
        let long = (item as AnyObject).value(forKey: "long")as! Double
        let annotation = Stands(latitude: lat, longitude: long)
        let tit = (item as AnyObject).value(forKey: "title") as! String
        let numb = (item as AnyObject).value(forKey: "no") as! Int
        annotation.title = "\(numb) \(tit)"
        annotation.no = numb
        annotations.append(annotation)
      }
    }
    return annotations
  }

然后是注解视图

 func mapView(_ mapView: MKMapView!, viewFor annotation: MKAnnotation!) -> MKAnnotationView! {


    let identifier = "Stand"


    var imageI: String?
    var imageAn: String?
    imageI = annotation.title!
    if annotation.isKind(of: Stands.self) {
      print("ANNOTATION \(imageI!)")
      if let range = imageI?.components(separatedBy: " ") {

        var imageII = range[0]
        imageAn = "\(imageII).png"
        print (imageAn!)
      }

      var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

      if annotationView == nil {

        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)

        //
        annotationView?.image = UIImage(named: imageAn! )
        annotationView?.canShowCallout = true
        annotationView?.layer.zPosition = -1

        let btn = UIButton(type: .detailDisclosure)
        annotationView?.rightCalloutAccessoryView = btn
      } else {

        annotationView!.annotation = annotation
      }

      return annotationView
    }

    return nil
  }

【问题讨论】:

    标签: annotations swift3 mkannotationview


    【解决方案1】:

    我似乎找到了解决方案,更多的是通过反复试验而不是知道我在做什么。 我添加了 annotationView?.image = UIImage(named: imageAn!) 制作

    }else{
    annotationView?.image = UIImage(named: imageAn! )
    annotationView.annotation  = annotation
    }
    

    如果我理解正确的话,注释是坐标、标题和副标题的信息,没有关于显示的信息。 AnnotationView 设置注释的显示方式。使用图钉或自定义图像。 如果 AnnotationView 为 nill,则正确创建注释视图。如果注释离开视图又回来了,AnnotationView 不为零,因此系统会随机取一个视图来显示它。 添加 annotationView?.image = UIImage(named: imageAn!) 强制显示正确的图像。 如果此架构不正确,我相信有人会纠正我。

    【讨论】:

    • 对于任何想要做同样事情的人,即有编号的注释,我现在改变了我的攻击路线。这个问题给了我这个想法。 stackoverflow.com/questions/9822756/… 制作带有标题的小矩形(我只是做了一个数字)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多