【发布时间】:2019-09-06 14:16:48
【问题描述】:
我的项目包含一个collectionView,每个单元格中都有一个地图..
我正在尝试自定义标记(注释),但它不起作用。
我就是这样做的:
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),所以我的注释从不定制:/
我认为我在某个地方错了,但我不知道在哪里..
你有什么想法吗?
谢谢你:)
【问题讨论】:
标签: swift swift4 mkmapview uicollectionviewcell mkannotation