CallOutAccessory按钮图像状态

问题描述

我在标注附件上有一个收藏夹按钮,该开关通过子类化UIButton来直观地工作(此按钮可处理图像开关)。当我四处移动地图时,我假设 重新绘制视图后,切换按钮将(通常会)返回到其原始未切换状态。 有人对这种发展有经验吗?预先感谢。

CallOutAccessory with fave button

解决方法

您可以在地图上的viewForAnnotation方法中进行处理。假设您已经归类为MKAnnotation,并且正在使用可重用的MKAnnotationView。在注释类中添加一个标志,以确定是否选择了注释并保持更新。然后,在您的viewForAnnotation方法中...

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = yourMap.dequeueReusableAnnotationView(withIdentifier: "someIdentifier") as! YourCustomAnnotationClass
    if annotation.isSelected {
        // Setup your button here
    }
    return annotationView
}

基本上,如果您使用的是可重用的批注视图,则必须设置类似于集合视图工作方式的视图。每次调用viewFor annotation时,该代码都会运行,因此,如果您每次都没有正确地手动设置它,或者对单元的重用不小心,则会发生一些奇怪的事情。