我的项目包含一个 collectionView,在每个单元格中有一个 map ..
我正在尝试自定义标记(注释),但它不起作用。
我是这样的:
1/我类 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/我类有趣的函数 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/扩展中有趣的功能:
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 的委托(delegate),您已在单元格中实现了委托(delegate)回调。您是否尝试过设置 mapView.delegate = self(在 collectionView 单元格中)
关于swift - CollectionViewCell、MapView 和自定义注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823780/