2017-05-21 13 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 
    } 
} 
또한

I이 확장 가지고

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

I는지도보기 매 10 초 어레이 내 데이터를 로딩하고이처럼 선물 :

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

오류 : - 내가 새로운 데이터를로드 할 때마다, 핀의 내 색깔은 다르지만 내 나타나서 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에 대해 더 알고 있습니다 =) –