title: iOS Swift UICollectionView registerNib for reuseable view not working
body: <p>I am trying to use a create a iCalendar lookalike using a UICollectionViewController. Right now I am trying to add reuseable views as column headers for each day.</p>
  
  <p>I made a storyboard and added a UICollectionViewController which is linked to this class:</p>
  
  <pre><code>import UIKit
  
  class WeekViewController: UICollectionViewController, UICollectionViewDelegate
  {
      let dataSource = WeekViewDataSource()
      let weekLayout = WeekViewLayout()
  
      override func awakeFromNib() {
          var columnHeaderViewNIB = UINib(nibName: "ColumnHeaderView", bundle: nil)
          self.collectionView?.registerNib(columnHeaderViewNIB, forSupplementaryViewOfKind: columnHeaderViewIdentifier, withReuseIdentifier: columnHeaderViewIdentifier)
  
          self.collectionView?.collectionViewLayout = weekLayout
          self.collectionView?.dataSource = dataSource
  
          super.awakeFromNib()
      }
  }
  </code></pre>
  
  <p>The ColumnHeaderView nib is actually a xib file with a view containing a single label. This view is linked to this class:</p>
  
  <pre><code>import UIKit
  
  class ColumnHeaderView: UICollectionReusableView
  {
      @IBOutlet weak var label: UILabel!
  }
  </code></pre>
  
  <p>The datasource and the layout:</p>
  
  <pre><code>import UIKit
  
  class WeekViewDataSource: NSObject, UICollectionViewDataSource {
  
      var events = Array&lt;String&gt;()
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int
      {
          return 0
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
          return UICollectionViewCell()
      }
  
      func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -&gt; UICollectionReusableView {
          var view: UICollectionReusableView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: kind, forIndexPath: indexPath) as UICollectionReusableView
  
          configureHeader(view, kind: kind, indexPath: indexPath)
  
          return view
      }
  
      func configureHeader(view: UICollectionReusableView, kind: String, indexPath: NSIndexPath)
      {
          if kind == columnHeaderViewIdentifier
          {
              let interval = NSTimeInterval(indexPath.item * 24 * 60 * 60)
              let now = NSDate().dateByAddingTimeInterval(interval)
              let format = NSDateFormatter()
              format.dateStyle = NSDateFormatterStyle.MediumStyle
              format.timeStyle = NSDateFormatterStyle.MediumStyle
              format.locale = NSLocale()
              format.dateFormat = "EE\ndd. MMMM"
  
              let headerView: ColumnHeaderView = view as ColumnHeaderView
  
              headerView.label.text = format.stringFromDate(now)
          }
      }
  }
  
  
  
  
  class WeekViewLayout: UICollectionViewLayout
  {
      var DaysPerWeek = 7
      var HoursPerDay = 24
      var HeightPerHour: CGFloat = 60.0
      var DayHeaderHeight: CGFloat = 100.0
      var DayHeaderWidth: CGFloat = 120.0
      var HourHeaderWidth: CGFloat = 50.0
  
      override func collectionViewContentSize() -&gt; CGSize {
          var contentWidth = HourHeaderWidth + DayHeaderHeight * CGFloat(DaysPerWeek)
          var contentHeight = DayHeaderHeight + HeightPerHour * CGFloat(HoursPerDay)
  
          return CGSizeMake(contentWidth, contentHeight)
      }
  
      override func layoutAttributesForElementsInRect(rect: CGRect) -&gt; [AnyObject]? {
          var layoutAttributes = Array&lt;UICollectionViewLayoutAttributes&gt;()
  
          // Column Header - Days
          var dayHeaderViewIndexPaths = indexPathsOfDayHeaderViewsInRect(rect)
          for indexPath in dayHeaderViewIndexPaths
          {
              var attributes = layoutAttributesForSupplementaryViewOfKind(columnHeaderViewIdentifier, atIndexPath: indexPath)
              layoutAttributes.append(attributes)
          }
  
          return layoutAttributes
      }
  
      override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewLayoutAttributes! {
          var attributes = UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, withIndexPath: indexPath)
  
          if elementKind == columnHeaderViewIdentifier
          {
              attributes.frame = CGRectMake(
                  HourHeaderWidth + DayHeaderWidth * CGFloat(indexPath.length),
                  self.collectionView!.contentOffset.y,
                  DayHeaderWidth,
                  DayHeaderHeight)
          }
  
          return attributes
      }
  
  
      func indexPathsOfDayHeaderViewsInRect(frame: CGRect) -&gt; [NSIndexPath]
      {
          var minDayIndex = dayIndexFromXCoord(CGRectGetMinX(frame))
          var maxDayIndex = dayIndexFromXCoord(CGRectGetMaxX(frame))
  
          var indexPaths = Array&lt;NSIndexPath&gt;()
          for i in (minDayIndex...maxDayIndex)
          {
              indexPaths.append(NSIndexPath(index: i))
          }
  
          return indexPaths
      }
  
  
      /*
          Helper methods
      */
  
      func dayIndexFromXCoord(positionX: CGFloat) -&gt; Int
      {
          var contentWidth = collectionViewContentSize().width - HourHeaderWidth
          var widthPerDay = contentWidth / CGFloat(DaysPerWeek)
          return Int(max(0.0, (positionX - HourHeaderWidth) / widthPerDay))
      }
  }
  </code></pre>
  
  <p>I am not that far yet because I am stuck at a very basic problem.</p>
  
  <pre><code>The ViewController doesn't register the nib and I always get this error:
  2014-09-22 22:49:52.242 WeekView[4973:304060] *** Assertion failure in -[WeekView.WeekViewLayout _decorationViewForLayoutAttributes:], /SourceCache/UIKit_Sim/UIKit-3318/UICollectionViewLayout.m:1292
  2014-09-22 22:49:52.244 WeekView[4973:304060] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a decoration view of kind: ColumnHeaderView - must register as a class or nib or connect a prototype in a storyboard'
  *** First throw call stack:
  (
      0   CoreFoundation                      0x0000000100aa13f5 __exceptionPreprocess + 165
      1   libobjc.A.dylib                     0x0000000102812bb7 objc_exception_throw + 45
      2   CoreFoundation                      0x0000000100aa125a +[NSException raise:format:arguments:] + 106
      3   Foundation                          0x000000010117b28f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
      4   UIKit                               0x0000000101bb699a -[UICollectionViewLayout _decorationViewForLayoutAttributes:] + 872
      5   UIKit                               0x0000000101b982e6 -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] + 413
      6   UIKit                               0x0000000101b994da -[UICollectionView _updateVisibleCellsNow:] + 3483
      7   UIKit                               0x0000000101b9d161 -[UICollectionView layoutSubviews] + 243
      8   UIKit                               0x00000001015e7199 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
      9   QuartzCore                          0x0000000105f6cf98 -[CALayer layoutSublayers] + 150
      10  QuartzCore                          0x0000000105f61bbe _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
      11  QuartzCore                          0x0000000105f61a2e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
      12  QuartzCore                          0x0000000105ecfade _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
      13  QuartzCore                          0x0000000105ed0bea _ZN2CA11Transaction6commitEv + 390
      14  UIKit                               0x000000010156c67d -[UIApplication _reportMainSceneUpdateFinished:] + 44
      15  UIKit                               0x000000010156d368 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642
      16  UIKit                               0x000000010156bd22 -[UIApplication workspaceDidEndTransaction:] + 179
      17  FrontBoardServices                  0x000000010507d2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
      18  CoreFoundation                      0x00000001009d6abc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
      19  CoreFoundation                      0x00000001009cc805 __CFRunLoopDoBlocks + 341
      20  CoreFoundation                      0x00000001009cbfc3 __CFRunLoopRun + 851
      21  CoreFoundation                      0x00000001009cba06 CFRunLoopRunSpecific + 470
      22  UIKit                               0x000000010156b799 -[UIApplication _run] + 413
      23  UIKit                               0x000000010156e550 UIApplicationMain + 1282
      24  WeekView                            0x00000001008afc0e top_level_code + 78
      25  WeekView                            0x00000001008afc4a main + 42
      26  libdyld.dylib                       0x0000000102fdd145 start + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException
  </code></pre>
  
  <p>Why is this happening?
  Am I missing anything?</p>
  
  <p>Any help would be appreachiated :) Thank you in advance</p>
  
  <p>EDIT:
  I found the problem!
  This line was wrong: </p>
  
  <pre><code>UICollectionViewLayoutAttributes(forDecorationViewOfKind: elementKind, withIndexPath: indexPath)
  </code></pre>
  
  <p>I tried to get the ViewLayoutAttributes for a decoration view but it is registered as a reuseable view. I changed this line and it works again.</p>