2017-05-21 2 views
1

Ich habe eine Funktion für tintColor in meiner Klasse:tintColor - zufällige Farbe, warum?

func pinColor() -> UIColor { 
    switch status.text! { 
    case "online": 
     return UIColor(red: 46/255, green: 204/255, blue: 113/255, alpha: 1.0) 
    default: 
     return UIColor.red 
    } 
} 

Auch ich habe diese Erweiterung:

extension ViewController: MKMapViewDelegate { 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? Marker { 
     let identifier = "pin" 
     var view: MKPinAnnotationView 
     if let dequeuedView = map.dequeueReusableAnnotationView(withIdentifier: identifier) 
      as? MKPinAnnotationView { 
      dequeuedView.annotation = annotation 
      view = dequeuedView 
     } else { 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      view.canShowCallout = true 
      view.calloutOffset = CGPoint(x: -5, y: 5) 
      view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView 
      view.pinTintColor = annotation.pinColor() 
     } 
     return view 
    } 
    return nil 
} 
} 

ich für mapView meine Daten mit Array laden alle 10 Sekunden und es wie folgt vorstellen:

func mapView() { 
    map.layoutMargins = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30) 
    map.showAnnotations(markersArrayFiltered!, animated: true) 
} 

Fehler: - jedes Mal, wenn ich neue Daten geladen werden, meine Farben der Pins sind unterschiedlich, aber mein dat a ändert sich nicht.

Watch example video of error

Was habe ich falsch gemacht?

Antwort

2

Sie verwenden dequeueReusableAnnotationView, die eine wiederverwendbare Anmerkungsansicht zurückgibt, die sich nach ihrem Bezeichner befindet.

Aber Sie setzen die PIN-Farbe nur für das erste Mal, dass Sie die MKPinAnnotationView initialisiert haben. Sie müssen in beiden Situationen einstellen. Und das nicht nur für die Pin-Farbe, für alles, was auf Daten basiert.

extension ViewController: MKMapViewDelegate { 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? Marker { 
     let identifier = "pin" 
     var view: MKPinAnnotationView 
     if let dequeuedView = map.dequeueReusableAnnotationView(withIdentifier: identifier) 
      as? MKPinAnnotationView { 
      dequeuedView.annotation = annotation 
      view = dequeuedView 
     } else { 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 

     } 
     view.canShowCallout = true 
     view.calloutOffset = CGPoint(x: -5, y: 5) 
     view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView 
     view.pinTintColor = annotation.pinColor() 
     return view 
    } 
    return nil 
} 
} 
+0

Vielen Dank! Jetzt funktioniert alles wie es soll. Jetzt weiß ich über mapKit mehr =) –

Verwandte Themen