删除标题等于/不等于字符串的注释? [英] Remove annotations containing a title equal/not equal to a String?

查看:83
本文介绍了删除标题等于/不等于字符串的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几天尝试删除注释,该注释的标题等于或不等于从另一个视图控制器的uicollection视图单元didSelect中选择的字符串.我将字符串传递给包含mapview的视图控制器.我使用自定义注释,该注释是如何显示注释的模型.

I have looked for a couple of days on trying to remove annotations that a title equal or not equal to a string that is selected from a uicollection View cell didSelect from another view controller. I get the string passed to my view controller that contains my mapview. I use a custom annotation that is the model for how the annotations are displayed.

如何按标题选择和删除自定义注释.我已经有一个字典数组,其中包含一旦删除其他注释后注释将使用的数据.我知道如何删除所有注释,但是不知道如何删除标题等于或不等于搜索字符串的注释.

How do I select and remove the custom annotations by their title. I already have an array of dictionaries that contains the data the annotations will use once the other annotations are removed. I know how to remove ALL the annotations, but not how to just remove the ones that have titles that are equal/not equal to the search string.

为什么网络上没有此类功能的swift 3?

Why is nothing on the web or current for swift 3 for such a function?

我想出了这个,但是只删除了注释,并显示了"filteredAnnotations"

I have come up with this, but only removed annotations and does display the "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语句仅返回一个空数组"[]"

using the print statement only returns an empty array of "[]"

推荐答案

使用filter获取应删除的所有注释的列表(即其标题不是您的搜索字符串,但不是MKUserLocation (或者),然后将其删除.

Use filter to get a list of all annotations that should be removed (i.e. whose title is not your search string, but isn't a MKUserLocation, either) and then remove them.

在Swift 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)

很显然,可以将!=更改为==以满足您的要求或其他要求,但这说明了使用filter标识标题与某些特定条件匹配的注释集的基本思想.

Obviously, change that != to == as suits your requirements, or whatever, but this illustrates the basic idea of using filter to identify a set of annotations whose title matches some particular criteria.

对于Swift 2,请参见此答案的先前版本.

For Swift 2, see previous revision of this answer.

这篇关于删除标题等于/不等于字符串的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆