title: Get indexPath from UICollectionViewController to UICollectionViewCell subclass
body: <p>in my view controller I Am loading a custom CollectionViewCell with subclass. Based on the position of a cell's indexpath I want to format the text labels differently. I.e. first row has only one cell with bigger text, whereas the second has two cell with smaller text.</p>
  
  <p>How can I access the indexpath from my UICollectionView in my UICollectionViewCell subclass? I tried a delegate protocol but this always returns nil.</p>
  
  <p>Code below and Thanks so much!</p>
  
  <p>Markus</p>
  
  <p>UICollectionViewController:</p>
  
  <pre><code>import UIKit
  
  protocol WorkoutDataViewControllerCVDataSource: AnyObject {
  
  func workoutType(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -&gt; WorkoutType
  func workoutDistance(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -&gt; Double
  func workoutDuration(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -&gt; Double
  func workoutInstantVelocity(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -&gt; Double
  }
  
  final class WorkoutDataViewControllerCV: UIViewController {
  
  @IBOutlet weak var collectionView: UICollectionView!
  
  weak var dataSource: WorkoutDataViewControllerCVDataSource!
  
  private lazy var velocityFormatter = VelocityFormatter(dataSource: self, delegate: self)
  private lazy var averageVelocityFormatter = VelocityFormatter(dataSource: self, delegate: self)
  
  override func viewDidLoad() {
      super.viewDidLoad()
      self.collectionView.register(MeasurementCollectionViewCell.preferredNib, forCellWithReuseIdentifier: MeasurementCollectionViewCell.preferredReuseIdentifier)
  }
  
   }
  
     // MARK: - Managing UICollectionView
  
   extension WorkoutDataViewControllerCV: UICollectionViewDataSource {
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return 4
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Measurement Cell", for: indexPath)
      return cell
       }
  
      }
  
     extension WorkoutDataViewControllerCV: UICollectionViewDelegateFlowLayout {
  
  func collectionView(_ collectionView: UICollectionView,
                      layout collectionViewLayout: UICollectionViewLayout,
                      sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
      let availableWidth = self.view.frame.width
  
      switch indexPath.row {
      case 0: return CGSize(width: availableWidth, height: 150)
      case 1: return CGSize(width: availableWidth/2.1, height: 150)
      case 2: return CGSize(width: availableWidth/2.1, height: 150)
      case 3: return CGSize(width: availableWidth, height: 150)
      default:
          return CGSize(width: availableWidth/2.1, height: 150)
      }
  }
  }
  
     // MARK: - Managing VelocityFormatter
  
     extension WorkoutDataViewControllerCV: VelocityFormatterDataSource     {
  
  func duration(for velocityFormatter: VelocityFormatter) -&gt; Double {
      return dataSource.workoutDuration(for: self)
  }
  
  func distance(for velocityFormatter: VelocityFormatter) -&gt; Double {
      return dataSource.workoutDistance(for: self)
  }
  
  func instantVelocity(for velocityFormatter: VelocityFormatter) -&gt; Double {
      return dataSource.workoutInstantVelocity(for: self)
  }
  }
  </code></pre>
  
  <p><strong>UICollectionViewCell.swift</strong></p>
  
  <pre><code>    import UIKit
  
    final class MeasurementCollectionViewCell: UICollectionViewCell {
  
  @IBOutlet private var measurementPropertyLabel: UILabel!
  @IBOutlet private var measurementValueLabel: UILabel!
  @IBOutlet private var measurementUnitLabel: UILabel!
  
  static let preferredReuseIdentifier = "Measurement Cell"
  static let preferredNib = UINib(nibName: "MeasurementCollectionViewCell", bundle: nil)
  
  override func awakeFromNib() {
      super.awakeFromNib()
      updateMeasurement(property: "Speed", value: "100", unit: "km/h")
  
      //measurementValueLabel.font = measurementValueLabel.font.monospacedDigitFont
  }
  
  func updateMeasurement(property: String, value: String, unit: String?) {
      measurementPropertyLabel.text = property
      measurementValueLabel.text = value
      measurementUnitLabel.text = unit
         }
  
      }
  </code></pre>