Hug*_*o75 2 annotations mapkit mkannotation swift
我的项目是带有很多注释点的地图,由于有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)
}
Run Code Online (Sandbox Code Playgroud)
我曾尝试实现这一点,但没有成功...
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"
}
Run Code Online (Sandbox Code Playgroud)
我有这个错误:
无法转换类型为'String ??'的值 到预期的参数类型'UIContentSizeCategory'
任何提示将不胜感激。
我知道这个问题回答有点老了,但是对于其他有问题的人来说,问题是String不能是可选的。修复它以提供默认值或强制解开它。例如,这将是该问题的一种解决方案:
let sortedNames = self.mapView.annotations.sorted(by: { ($0.title ?? "") < ($1.title ?? "") }))
return sortedNames[row].title ?? "No title"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
473 次 |
| 最近记录: |