【发布时间】:2018-07-09 23:08:40
【问题描述】:
我的项目是一个有很多注释点的地图,用户可以通过pickerView来查找特定的注释。一切都很好,除了所有注释点的列表似乎是随机显示的。我想按字母顺序对注释进行排序。
这是我当前的工作代码:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.mapView.annotations[row].title ?? "No title"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.mapView.selectAnnotation(self.mapView.annotations[row], animated: true)
}
我尝试过实现这个,但没有任何成功...
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let sortedNames = self.mapView.annotations.sorted(by: {$0.title < $1.title})
return sortedNames[row].title ?? "No title"
}
我遇到了这个错误:
无法转换“String??”类型的值到预期的参数类型 'UIContentSizeCategory'
任何提示将不胜感激。
【问题讨论】:
-
为什么不只对用于创建注释的名称列表进行排序,而不是尝试对注释进行排序?值得指出的是,
sortedNames实际上是一个按标题排序的MKMapAnnotation数组。如果你真的需要这样做,你可以把它变成这样的字符串数组:let sortedNames = self.mapView.annotations.sorted(by: {$0.title < $1.title}).flatMap({ $0.title }) -
对于选择器中的每一行按字母顺序对整个列表进行排序似乎也不是很聪明。为什么不只维护一个标题列表,以便您可以排序一次,然后在为您的选择器获取标题时继续访问该列表?
-
因为每次注解的顺序都变了,所以我提出了注解的请求……我只有25个注解。
标签: swift annotations mapkit mkannotation