从地图中删除选定的 MKAnnotation 时删除错误显示的注释 SWIFT 4.1
Deleting wrong displayed annotation when deleting selected MKAnnotation from map SWIFT 4.1
我有一个 userAlertAnnotationArray: [MKAnnotations]
可以从从 Firebase 检索到的数据中获取条目。删除部分与 Firebase 中的帖子和数组中的条目完美配合,但从地图中删除选定的 MKAnnotation 时失败。它从地图中删除了错误的注释。
如果我将 mapVC 退出到 menuVC 并重新输入 mapVC,则正确的注释消失了。
尝试几次后,我注意到了它,因为只有当我重新输入 mapVC 时才会出现超出范围的错误。我不想调用 viewDiedLoad() 函数来刷新视图,因为每次删除注释时,这将是 Firebase 的每个条目的新读取。
你能看出我哪里写错了吗?更新显示注释失败,userAlertAnnotationArray 删除注释。如果您需要查看更多代码或其他功能,请告诉我。提前致谢。这是 didSelect 函数:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let alertController = UIAlertController(title: "User Alert", message: "Delete selected Alert?", preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
var selectedAnnotation = view.annotation
if let annotation = view.annotation as? UserAlert {
let key = annotation.firebaseKey
let index = (self.mapView.annotations as NSArray).index(of: annotation)
self.userAlertNotificationArray.remove(at: index)
print(" New alert array after deliting is : \(self.userAlertNotificationArray)" )
print("post to remove key is: \(String(describing: key!))")
self.ref?.child("Community").child("Alert Notifications").child("\(String(describing: annotation.firebaseKey!))").removeValue()
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(self.userAlertNotificationArray)
// self.viewDidLoad()
}
// print("Ok button tapped")
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
print("Cancel button tapped");
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)
}
问题是您是从地图视图上的注释数组而不是您的数组获取索引;它们可能不同,例如,如果地图显示的用户位置是另一个注释。
尝试将 index
的声明替换为:
let index = (self.userAlertNotificationArray as NSArray).index(of: annotation)
我有一个 userAlertAnnotationArray: [MKAnnotations]
可以从从 Firebase 检索到的数据中获取条目。删除部分与 Firebase 中的帖子和数组中的条目完美配合,但从地图中删除选定的 MKAnnotation 时失败。它从地图中删除了错误的注释。
如果我将 mapVC 退出到 menuVC 并重新输入 mapVC,则正确的注释消失了。
尝试几次后,我注意到了它,因为只有当我重新输入 mapVC 时才会出现超出范围的错误。我不想调用 viewDiedLoad() 函数来刷新视图,因为每次删除注释时,这将是 Firebase 的每个条目的新读取。
你能看出我哪里写错了吗?更新显示注释失败,userAlertAnnotationArray 删除注释。如果您需要查看更多代码或其他功能,请告诉我。提前致谢。这是 didSelect 函数:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let alertController = UIAlertController(title: "User Alert", message: "Delete selected Alert?", preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
var selectedAnnotation = view.annotation
if let annotation = view.annotation as? UserAlert {
let key = annotation.firebaseKey
let index = (self.mapView.annotations as NSArray).index(of: annotation)
self.userAlertNotificationArray.remove(at: index)
print(" New alert array after deliting is : \(self.userAlertNotificationArray)" )
print("post to remove key is: \(String(describing: key!))")
self.ref?.child("Community").child("Alert Notifications").child("\(String(describing: annotation.firebaseKey!))").removeValue()
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(self.userAlertNotificationArray)
// self.viewDidLoad()
}
// print("Ok button tapped")
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
print("Cancel button tapped");
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)
}
问题是您是从地图视图上的注释数组而不是您的数组获取索引;它们可能不同,例如,如果地图显示的用户位置是另一个注释。
尝试将 index
的声明替换为:
let index = (self.userAlertNotificationArray as NSArray).index(of: annotation)