2017-05-21 12 views
1

私は私のクラスでtintColorする機能を持っている:tintColor - ランダムな色、なぜですか?

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 
    } 
} 

また、私はこの拡張機能を持っている:

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 
} 
} 

私は10秒ごとのMapViewの配列と私のデータをロードし、このようにそれを提示:

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

エラー: - たびに私は新しいデータをロードする際に、ピンの私の色が異なっているが、私のDAT aは変わらない。

Watch example video of error

私が間違って何をしたのですか?

答えて

2

dequeueReusableAnnotationViewを使用しています。再利用可能な注釈ビューを識別子で返します。

ただし、MKPinAnnotationViewを初めて初期化したときにのみピンの色を設定します。両方の状況で設定する必要があります。これはピンカラーだけでなく、データに基づいたすべてのものです。

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

ありがとうございました!今はすべてが正常に動作します。今私はmapKitについてもっと知っている=) –

関連する問題