注释的重用标识符的意义是什么

时间:2018-09-19 06:48:14

标签: ios mapkit reuseidentifier

我正在尝试在我正在使用的mapkit上实现注释

mapView.register(EventMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

但是,我经常看到这种格式

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annotation = annotation as? Event else { return nil }

    let identifier = "marker"
    var view: MKMarkerAnnotationView

    if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        as? MKMarkerAnnotationView {
        dequeuedView.annotation = annotation
        view = dequeuedView
    } else {
        view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        view.canShowCallout = true
        view.calloutOffset = CGPoint(x: -5, y: 5)
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }
    return view
}

如果注释类型不止一种,我会使用后一种形式吗?

我也很好奇我的MKMapViewDefaultAnnotationViewReuseIdentifier是否有效?我没有选择它,但是由于我只有1种注释类型,没关系,对吗?

最后,重用标识符是否仍具有dequeueReusableAnnotationView(withIdentifier: identifier)的功能?

1 个答案:

答案 0 :(得分:0)

这里有很多事情要考虑:

  • MKMapViewDefaultAnnotationViewReuseIdentifier仅在 iOS 11 + 上受支持,因此,如果要定位较早的iOS,则可能要使用后一种形式。
  • 类似于后一种形式的方法不仅可以在注释类型不只一种的情况下实现,而且可以很好地实现。使用重用标识符的要点是,每当注释离开屏幕时,便可以在其他注释中重用它,这有时可以节省大量资源。

底线:

如果仅定位到iOS 11+,并且只有1个注释,则可以使用MKMapViewDefaultAnnotationViewReuseIdentifier。否则请考虑其他形式。

希望这可以帮助您做出决定。