ios - MKAnnotationView 未从 MKMapView 中删除

标签 ios swift mkmapview mkannotationview

我有一个简单的数组或 MKAnnotations,用于在 View 加载时从 CoreData 获取的位置对象。当我删除一个位置对象时,我也手动从数组中删除了位置对象。从数组中删除后,我调用 removeAnnotations() 然后基于数组调用 addAnnotations()。我注意到 MKAnnotationView 不再位于已删除的位置,但它并未从 MapView 中删除,仅移动到 0º lat 0º lon。

我不确定为了让它从 MapView 中完全删除我做错了什么。

*** 注意:我正在学习教程,对于学习过程,我手动更新位置数组而不是使用 NSFetchedResultsController。

想法?

代码如下:

var managedObjectContext: NSManagedObjectContext! {
    didSet {
        NSNotificationCenter.defaultCenter().addObserverForName(NSManagedObjectContextObjectsDidChangeNotification, object: managedObjectContext, queue: NSOperationQueue.mainQueue()) { notification in
            if self.isViewLoaded() {
                if let dictionary = notification.userInfo {

                    if dictionary["inserted"] != nil {

                        print("*** Inserted")
                        let insertedLocationSet: NSSet = dictionary["inserted"] as! NSSet
                        let insertedLocationArray: NSArray = insertedLocationSet.allObjects
                        let insertedLocation = insertedLocationArray[0] as! Location
                        self.locations.append(insertedLocation)

                    } else if dictionary["deleted"] != nil {

                        print("*** Deleted")
                        let deletedLocationSet = dictionary["deleted"] as! NSSet
                        let deletedLocationArray = deletedLocationSet.allObjects
                        let deletedLocation = deletedLocationArray[0] as! Location

                        if let objectIndexInLocations = self.locations.indexOf(deletedLocation) {

                            self.locations.removeAtIndex(objectIndexInLocations)

                        }

                    }
                }
            }

            self.drawAnnotations()

        }
    }
}

var locations = [Location]()


func updateLocations() {  // called by viewDidLoad()

    let entity = NSEntityDescription.entityForName("Location", inManagedObjectContext: managedObjectContext)

    let fetchRequest = NSFetchRequest()
    fetchRequest.entity = entity

    locations = try! managedObjectContext.executeFetchRequest(fetchRequest) as! [Location]

    drawAnnotations()

}

func drawAnnotations() {

    mapView.removeAnnotations(locations)
    mapView.addAnnotations(locations)

}

最佳答案

你的问题是,当你执行 mapView.removeAnnotations(locations) 时,locations 数组已经更新,删除的注释不再在该数组中,所以它没有被删除。您可以通过引用 MapView 本身的 annotations 属性来删除所有当前注释 -

func drawAnnotations() {
    mapView.removeAnnotations(mapView.annotations)
    mapView.addAnnotations(locations) 
}

关于ios - MKAnnotationView 未从 MKMapView 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34189208/

相关文章:

ios - 如何将 iOS slider 值限制为整数?

iphone - 如何使用 UIsearchBar 委托(delegate)方法关闭键盘?我尝试了所有方法,但没有用。

ios - 将数据从 UICollectionView 快速/传递到 DetailView/自定义按钮

iphone - 将 MKMapView 缩放至 CLRegion

ios - 将 FirebaseUI 定向到 iOS 应用程序中的电话登录

ios - Cordova 试图调用电话号码

iOS10 iMessage : Unable to insert data into iMessage using MSConversation

swift - 快速简化多个 if 字段验证

ios - 在不更改缩放级别或跨度的情况下更改 map 点的位置

ios - 如何在 MKMapView swift 中添加覆盖路径