CollectionViewCell,MapView和自定义注释

时间:2019-09-06 14:16:48

标签: swift swift4 mkmapview uicollectionviewcell mkannotation

我的项目包含一个collectionView,其中每个单元格中的地图 ..

map in cell

我正在尝试自定义标记(注释),但是它不起作用。

这是我的工作方式:

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 /这是我的物品的连接方式

delegate

我的问题是我从来没有进入扩展名(mapView / ViewFor),所以我的注释也从未定制过:/

我认为我在某个地方错了,但我不知道在哪里..

你有个主意吗?

谢谢:)

1 个答案:

答案 0 :(得分:1)

从图中很难看出,但似乎您已经将collectionView本身设置为mapView的委托,据我所知,您已在其中实现了单元格中的委托回调。您是否尝试过设置mapView.delegate = self(在collectionView单元格中)

相关问题