swift - CollectionViewCell、MapView 和自定义注释

标签 swift swift4 mkmapview uicollectionviewcell mkannotation

我的项目包含一个 collectionView,在每个单元格中有一个 map ..

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)所以我的注释从来没有被定制过:/

我想我哪里错了,但我不知道在哪里..

你有什么想法吗?

谢谢你:)

最佳答案

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

关于swift - CollectionViewCell、MapView 和自定义注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823780/

相关文章:

ios - 将数据从第二个 VC 传递到第一个 VC 并显示在文本字段 Swift4 中

ios - MKCoordinateRegionMakeWithDistance 抛出 "Thread 1: signal SIGABRT"

ios - 更改 mkmapview(删除注释)

iphone - iOS - 使用 MKMapView 弹出和推送 View Controller 时意外崩溃

swift - Swift 中的多可选类型

swift - iOS 13 奇怪的搜索 Controller 差距

ios - 将响应转换为字典时,已解析响应的顺序已更改

swift - UICollectionView 水平滚动与静态焦点项目

ios - Swift - 如何绘制一条线,其中每个点都可以有单独的颜色

ios - 将代码更新到 xcode 9 和 swift 4 后应用程序崩溃