choefele/CCHMapClusterController

willReuseMapClusterAnnotation does not work in Swift

Closed this issue · 6 comments

CCHMapClusterController works great with my Swift project except for one issue:

The willReuseMapClusterAnnotation does not seem to work in Swift. I believe that I've added all files correctly through CocoaPods & a bridging header and that the controller is set up like in the example but the statement willReuseMapClusterAnnotation does not get executed (No statements are printed inside the function).

I would be really thankful for any help!

MapViewController

func mapClusterController(mapClusterController: CCHMapClusterController, willReuseMapClusterAnnotation mapClusterAnnotation: CCHMapClusterAnnotation) {
      let clusterAnnotationView = (self.mapView.viewForAnnotation(mapClusterAnnotation) as! ClusterAnnotationView)
      clusterAnnotationView.count = mapClusterAnnotation.annotations.count
      clusterAnnotationView.isUniqueLocation = mapClusterAnnotation.isUniqueLocation()
      print(clusterAnnotationView.count)
    }

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

      var annotationView: MKAnnotationView?
      if (annotation is CCHMapClusterAnnotation) {
        let identifier = "clusterAnnotation"
        var clusterAnnotationView = (mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? ClusterAnnotationView)
        if clusterAnnotationView != nil {
          clusterAnnotationView!.annotation = annotation
        } else {
          clusterAnnotationView = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier)
          clusterAnnotationView!.canShowCallout = true
        }
        let clusterAnnotation = (annotation as! CCHMapClusterAnnotation)
        clusterAnnotationView!.count = clusterAnnotation.annotations.count
        clusterAnnotationView!.isUniqueLocation = clusterAnnotation.isUniqueLocation()
        annotationView = clusterAnnotationView
      }
      return annotationView
    }

ClusterAnnotationView

import Foundation
  import CCHMapClusterController
  import MapKit

  class ClusterAnnotationView : MKAnnotationView {

    var count: Int!
    var countLabel: UILabel!
    var fontSize: CGFloat = 12
    var borderWidth: CGFloat = 3
    var isUniqueLocation = false

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
      super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

      let clusterAnnotation: CCHMapClusterAnnotation = annotation as! CCHMapClusterAnnotation
      let clusterCount = clusterAnnotation.annotations.count
      self.backgroundColor = UIColor.clearColor()
      setupLabel()
      setupCount(clusterCount)
    }

    required override init(frame: CGRect) {
      super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
    }

    func setupLabel() {
      self.countLabel = UILabel(frame: bounds)
      self.countLabel.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
      self.countLabel.textAlignment = .Center
      self.countLabel.backgroundColor = UIColor.clearColor()
      self.countLabel.textColor = UIColor.whiteColor()
      self.countLabel.adjustsFontSizeToFitWidth = true
      self.countLabel.minimumScaleFactor = 2
      self.countLabel.numberOfLines = 1
      self.countLabel.font = UIFont.boldSystemFontOfSize(self.fontSize)
      self.countLabel.baselineAdjustment = .AlignCenters
      addSubview(self.countLabel)
    }

    func setupCount(count: Int) {
      self.count = count
      self.countLabel.text = "\(count)"
      self.setNeedsLayout()
    }

    func setUniqueLocation(isUniqueLocation: Bool) {
      self.isUniqueLocation = isUniqueLocation
      self.setNeedsLayout()
    }

    override func layoutSubviews() {

      var imageName = ""
      countLabel?.frame = self.bounds
      centerOffset = CGPointZero

      layer.borderColor = UIColor.whiteColor().CGColor
      layer.borderWidth = borderWidth
      layer.cornerRadius = self.bounds.size.width / 2

      if self.isUniqueLocation {
        imageName = "questionButton.png"
      } else {
        switch count {
        case 0...5:
          fontSize = 12
          imageName = "clusterSmall.png"
          borderWidth = 3

        case 6...15:
          fontSize = 13
          imageName = "clusterMedium.png"
          borderWidth = 4

        default:
          fontSize = 14
          imageName = "clusterLarge.png"
          borderWidth = 5
        }
      }
      self.image = UIImage(named: imageName)
    }

  }

Would you mind creating a small sample project that reproduces this problem (in Swift)? This would help me find the problem

Here you go, thanks for your help Claus!

CCHMapExampleProject.zip

Is this maybe happening due to the Bridging Header? I've also noticed that some methods like updateAnnotations are not available in my Swift project.

Hi @pkuklis – the only thing missing in your code example is self.mapClusterController.delegate = self in viewDidLoad (right after initialising the cluster controller) ;-)

It's always the little things, thanks @choefele :)

it will be very helpfull if you guys put this simple example on this repo too, swift devs will be able to see example. @pkuklis @choefele