我花了几天时间试图删除标题等于或不等于从另一个视图控制器的 uicollection 视图单元 didSelect 中选择的字符串的注释。我将字符串传递给包含地图视图的视图控制器。我使用自定义注释,它是注释显示方式的模型。
如何按标题选择和删除自定义注释。我已经有一个字典数组,其中包含删除其他注释后注释将使用的数据。我知道如何删除所有注释,但不知道如何删除标题等于/不等于搜索字符串的注释。
为什么网络上或当前的 swift 3 上没有任何此类功能?
我想出了这个,但只删除了注释并显示了“filteredAnnotations”
var filteredAnnotations = self.mapview.annotations.filter {($0.title != nil) && isEqual(searchString) }
print(filteredAnnotations)
self.mapview.removeAnnotations(self.mapview.annotations)
self.mapview.addAnnotations(filteredAnnotations)
使用 print 语句仅返回“[]”的空数组
Use filter
获取应删除的所有注释的列表(即其标题不是您的搜索字符串,但也不是MKUserLocation
,或者),然后删除它们。
在斯威夫特 3 中:
let filteredAnnotations = mapView.annotations.filter { annotation in
if annotation is MKUserLocation { return false } // don't remove MKUserLocation
guard let title = annotation.title else { return false } // don't remove annotations without any title
return title != searchString // remove those whose title does not match search string
}
mapView.removeAnnotations(filteredAnnotations)
显然,改变它!=
to ==
适合您的要求,或者其他什么,但这说明了使用的基本思想filter
识别标题符合某些特定条件的一组注释。
对于 Swift 2,请参阅此答案的先前修订版 https://stackoverflow.com/revisions/40594586/3.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)