CollectionViewCell、MapView 和自定义注释
CollectionViewCell, MapView and Custom Annotation
我的项目包含一个带有地图的 collectionView 每个单元格..
我正在尝试自定义标记(注释),但它不起作用。
我是这样做的:
1/ 我的 class ListeIncidentsController 中有趣的功能:
class ListeIncidentsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate {
...
// for each cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCelluleIncident", for: indexPath) as! collectionViewCelluleIncident
let Incident = TabListeIncidents[indexPath.section] // section, pas row car on a mis les items en sections
cell.displayContent(Incident: Incident, isSelected: cell.isSelected)
return cell
}
...
}
2/ 我的 class collectionViewCelluleIncident 中有趣的函数:
class collectionViewCelluleIncident : UICollectionViewCell {
...
func displayContent(Incident: Incident, isSelected : Bool){
let TabCoordonnees = Incident.Coordonnee.split(separator: ",")
let coordonnees = CLLocationCoordinate2D(latitude: Double(TabCoordonnees[0])!, longitude: Double(TabCoordonnees[1])!)
let rayon : Double = Incident.Rayon
// Add my custom Annotation
let pinIncident = MyCustomPointAnnotation(TypeDePoint: .Incident) // class : CustomPointAnnotation.swift
pinIncident.title = Incident.Adresse
pinIncident.subtitle = "Rayon : "+String(Incident.Rayon)+"m"
pinIncident.coordinate = CLLocationCoordinate2D(latitude: coordonnees.latitude, longitude: coordonnees.longitude)
self.mapView.addAnnotation(pinIncident) // add my annotation
...etc etc
}
...
}
3/ extensions 中有趣的功能:
extension collectionViewCelluleIncident : MKMapViewDelegate {
...
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyCustomPin"
if annotation is MKUserLocation {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
annotationView?.image = nil
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
annotationView?.image = UIImage(named: "my_custom_icon")
}
else {
annotationView?.annotation = annotation
annotationView?.image = nil
}
return annotationView
}
}
4/ 这就是我的物品的连接方式
我的问题是我从来没有进入我的扩展(mapView/ViewFor)所以我的注释从来没有被定制过:/
我想我哪里错了,但我不知道在哪里..
你有想法吗?
谢谢:)
从图中看有点难看,但据我所知,你似乎已经将 collectionView 本身设置为 mapView 的委托,你已经在单元格中实现了委托回调.您是否尝试过设置 mapView.delegate = self(在 collectionView 单元格中)
我的项目包含一个带有地图的 collectionView 每个单元格..
我正在尝试自定义标记(注释),但它不起作用。
我是这样做的:
1/ 我的 class ListeIncidentsController 中有趣的功能:
class ListeIncidentsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate {
...
// for each cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCelluleIncident", for: indexPath) as! collectionViewCelluleIncident
let Incident = TabListeIncidents[indexPath.section] // section, pas row car on a mis les items en sections
cell.displayContent(Incident: Incident, isSelected: cell.isSelected)
return cell
}
...
}
2/ 我的 class collectionViewCelluleIncident 中有趣的函数:
class collectionViewCelluleIncident : UICollectionViewCell {
...
func displayContent(Incident: Incident, isSelected : Bool){
let TabCoordonnees = Incident.Coordonnee.split(separator: ",")
let coordonnees = CLLocationCoordinate2D(latitude: Double(TabCoordonnees[0])!, longitude: Double(TabCoordonnees[1])!)
let rayon : Double = Incident.Rayon
// Add my custom Annotation
let pinIncident = MyCustomPointAnnotation(TypeDePoint: .Incident) // class : CustomPointAnnotation.swift
pinIncident.title = Incident.Adresse
pinIncident.subtitle = "Rayon : "+String(Incident.Rayon)+"m"
pinIncident.coordinate = CLLocationCoordinate2D(latitude: coordonnees.latitude, longitude: coordonnees.longitude)
self.mapView.addAnnotation(pinIncident) // add my annotation
...etc etc
}
...
}
3/ extensions 中有趣的功能:
extension collectionViewCelluleIncident : MKMapViewDelegate {
...
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyCustomPin"
if annotation is MKUserLocation {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
annotationView?.image = nil
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
annotationView?.image = UIImage(named: "my_custom_icon")
}
else {
annotationView?.annotation = annotation
annotationView?.image = nil
}
return annotationView
}
}
4/ 这就是我的物品的连接方式
我的问题是我从来没有进入我的扩展(mapView/ViewFor)所以我的注释从来没有被定制过:/
我想我哪里错了,但我不知道在哪里..
你有想法吗?
谢谢:)
从图中看有点难看,但据我所知,你似乎已经将 collectionView 本身设置为 mapView 的委托,你已经在单元格中实现了委托回调.您是否尝试过设置 mapView.delegate = self(在 collectionView 单元格中)