title: Multiple Decoration Views added to UICollectionView
body: <p>I'm creating a basic collection view layout which subclasses <code>UICollectionViewFlowLayout</code>. However, I'm noticing that there appear to be several decoration views stacked on top of one another.</p>
  
  <p>Whenever the user selects an item in the last section, I'm adding a new section with the code below. It seems that whenever I execute this code, one additional copy of the decoration view is being added to each already existing section.</p>
  
  <pre><code>[collectionView performBatchUpdates:^{
      currentModelArrayIndex++;
      [collectionView insertSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex]];
      [collectionView reloadSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex-1]];
  } completion:^(BOOL finished) {
      [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:currentModelArrayIndex] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
  }];
  </code></pre>
  
  <p>I've corroborated this by giving my decoration views an <code>alpha</code> of <code>0.2f</code> and seeing them stack up. </p>
  
  <p><img src="https://i.stack.imgur.com/bQ21o.png" alt="enter image description here"></p>
  
  <p>I also performed a dump of the collection view hierarchy and saw 10 instances of <code>AFDecorationView</code> when I should only see 4:</p>
  
  <pre><code>   | &lt;AFDecorationView: 0x719ee50; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); alpha = 0; hidden = YES; layer = &lt;CALayer: 0x719eec0&gt;&gt;
     | &lt;AFDecorationView: 0x71ad980; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); alpha = 0; hidden = YES; layer = &lt;CALayer: 0x71adb60&gt;&gt;
     | &lt;AFDecorationView: 0x71afc90; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); layer = &lt;CALayer: 0x71afd60&gt;&gt;
     | &lt;AFDecorationView: 0xd79ac30; baseClass = UICollectionReusableView; frame = (0 596.406; 768 203.281); alpha = 0; hidden = YES; layer = &lt;CALayer: 0xd79ad00&gt;&gt;
     | &lt;AFDecorationView: 0xd79cf20; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); layer = &lt;CALayer: 0xd79cff0&gt;&gt;
     | &lt;AFDecorationView: 0xd79dac0; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); layer = &lt;CALayer: 0xd79a980&gt;&gt;
     | &lt;AFDecorationView: 0xd794fd0; baseClass = UICollectionReusableView; frame = (0 859.688; 768 225.938); layer = &lt;CALayer: 0xd7950a0&gt;&gt;
     | &lt;AFDecorationView: 0xd7a1300; baseClass = UICollectionReusableView; frame = (0 596.406; 768 203.281); layer = &lt;CALayer: 0xd7a13d0&gt;&gt;&lt;/CALayer:&gt;
     | &lt;AFDecorationView: 0xd7a35d0; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); layer = &lt;CALayer: 0xd794470&gt;&gt;
     | &lt;AFDecorationView: 0xd7a43e0; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); layer = &lt;CALayer: 0xd7a44b0&gt;&gt;
  </code></pre>
  
  <p>I have tried looking at <a href="https://github.com/mpospese/IntroducingCollectionViews" rel="noreferrer">another custom layout example</a>, but they seem to instantiate quite a few instances of their decoration views, too. Could this be a bug in <code>UICollecionView</code>? Or is it up to us to only add the decoration view layout attribute in <code>layoutAttributesForElementsInRect:</code> once per section?</p>
  
  <p>The decoration-related parts of custom layout look like the following.</p>
  
  <pre><code>@implementation AFCollectionViewFlowLayout
  
  -(id)init
  {
      if (!(self = [super init])) return nil;
  
      [self registerClass:[AFDecorationView class] forDecorationViewOfKind:AFCollectionViewFlowLayoutBackgroundDecoration];
  
      return self;
  }
  
  #pragma mark - Private Helper Methods
  
  -(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
  {
      //implemented
  }
  
  #pragma mark - Overridden Methods
  
  #pragma mark Cell Layout
  
  -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  {
      NSArray *attributesArray = [super layoutAttributesForElementsInRect:rect];
  
      NSMutableArray *newAttributesArray = [NSMutableArray array];
  
      for (UICollectionViewLayoutAttributes *attributes in attributesArray)
      {
          [self applyLayoutAttributes:attributes];
  
          if (attributes.indexPath.item == 0)
          {
              UICollectionViewLayoutAttributes *newAttributes = [self layoutAttributesForDecorationViewOfKind:AFCollectionViewFlowLayoutBackgroundDecoration atIndexPath:attributes.indexPath];
  
              [newAttributesArray addObject:newAttributes];
          }
      }
  
      attributesArray = [attributesArray arrayByAddingObjectsFromArray:newAttributesArray];
  
      return attributesArray;
  }
  
  #pragma mark Decoration View Layout
  
  -(UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath
  {
      UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
  
      if ([decorationViewKind isEqualToString:AFCollectionViewFlowLayoutBackgroundDecoration])
      {
          UICollectionViewLayoutAttributes *tallestCellAttributes;
          NSInteger numberOfCellsInSection = [self.collectionView numberOfItemsInSection:indexPath.section];
  
          for (NSInteger i = 0; i &lt; numberOfCellsInSection; i++)
          {
              NSIndexPath *cellIndexPath = [NSIndexPath indexPathForItem:i inSection:indexPath.section];
  
              UICollectionViewLayoutAttributes *cellAttribtes = [self layoutAttributesForItemAtIndexPath:cellIndexPath];
  
              if (CGRectGetHeight(cellAttribtes.frame) &gt; CGRectGetHeight(tallestCellAttributes.frame))
              {
                  tallestCellAttributes = cellAttribtes;
              }
          }
  
          CGFloat decorationViewHeight = CGRectGetHeight(tallestCellAttributes.frame) + self.headerReferenceSize.height;
  
          layoutAttributes.size = CGSizeMake([self collectionViewContentSize].width, decorationViewHeight);
          layoutAttributes.center = CGPointMake([self collectionViewContentSize].width / 2.0f, tallestCellAttributes.center.y);
          layoutAttributes.zIndex = -1;
      }
  
      return layoutAttributes;
  }
  
  @end
  </code></pre>
  
  <p>I only want one decoration view per section, so I'm adding the decoration view to the first section. </p>