title: iOS: Separate section background colors on UICollectionView
body: <p>I have a UICollectionView with a grey background color, however I want one of my sections to have a white background. I have discovered this was not as simple as I had hoped (i.e. the section having a attribute I could just set).</p>
  
  <p>From looking at <a href="https://stackoverflow.com/questions/13609204/how-to-change-background-color-of-a-whole-section-in-uicollectionview">How to change background color of a whole section in UICollectionView?</a> it was suggested to use a decoration view and this question <a href="https://stackoverflow.com/questions/12810628/uicollectionview-decoration-view">UICollectionView Decoration View</a> has helped me change the section color by subclassing <code>UICollectionReusableView</code> and <code>UICollectionViewFlowLayout</code>.</p>
  
  <p>My issue is that I cannot get the section height (which is dynamic) to set the UIView height, and also the beginning of my following section is blank until there is a reloadData call on it.</p>
  
  <p>Here is my code so far:
  <code>WhiteBackgroundCollectionReusableView Class</code>:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface WhiteBackgroundCollectionReusableView : UICollectionReusableView
  
  @end
  
  @implementation WhiteBackgroundCollectionReusableView
  
  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          // Initialization code
          [self setBackgroundColor:[UIColor redColor]];
      }
      return self;
  }
  
  @end
  </code></pre>
  
  <p><code>WhiteBackgroundCollectionViewFlowLayout Class</code>:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  #import "WhiteBackgroundCollectionReusableView.h"
  
  @interface WhiteBackgroundCollectionViewFlowLayout : UICollectionViewFlowLayout
  
  @end
  
  @implementation WhiteBackgroundCollectionViewFlowLayout
  
  - (id)init
  {
      self = [super init];
      if (self) {
          // Initialization code
  
          [self registerClass:[WhiteBackgroundCollectionReusableView class] forDecorationViewOfKind:@"WhiteSection"];
      }
      return self;
  }
  
  - (UICollectionViewLayoutAttributes*)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath*)indexPath
  {
      UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
  
      if(indexPath.section == 0) {
          layoutAttributes.frame = CGRectMake(0.0, 0.0, self.collectionViewContentSize.width, 100);
  
          layoutAttributes.zIndex = -1;
      }
  
      return layoutAttributes;
  }
  
  - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  {
      NSMutableArray *allAttributes = [[NSMutableArray alloc] initWithCapacity:4];
  
      [allAttributes addObject:[self layoutAttributesForDecorationViewOfKind:@"WhiteSection" atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]];
  
      for(NSInteger i = 0; i &lt; [self.collectionView numberOfItemsInSection:0]; i++)
      {
          NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
          UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
          [allAttributes addObject:layoutAttributes];
      }
  
      return allAttributes;
  }
  
  @end
  </code></pre>
  
  <p>I then assign my flow layout to my UICollectionView in my <code>UICollectionViewController</code> in <code>viewDidLoad</code>:</p>
  
  <pre><code>[_collectionView setCollectionViewLayout:[[WhiteBackgroundCollectionViewFlowLayout alloc] init]];
  </code></pre>
  
  <p>Any idea where I can go from here??</p>
  
  <p><strong>UPDATE:</strong>
  I've fixed the issue where the next section was not showing. To do this I needed to change the zIndex to 1, so my <code>layoutAttributesForElementsInRect</code> function now looks like this:</p>
  
  <pre><code>- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
  {
      NSArray *array = [super layoutAttributesForElementsInRect:rect];
  
      for(UICollectionViewLayoutAttributes *attributes in array) {
          attributes.zIndex = 1;
      }
  
      NSMutableArray *newArray = [array mutableCopy];
  
      [newArray addObject:[self layoutAttributesForDecorationViewOfKind:@"WhiteSection" atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]];
  
      for(NSInteger i = 0; i &lt; [self.collectionView numberOfItemsInSection:0]; i++) {
          NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
          UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
          [newArray addObject:layoutAttributes];
      }
  
      array = [NSArray arrayWithArray:newArray];
  
      return array;
  }
  </code></pre>
  
  <p>So for now all I need is a way of getting the height of a section! PLEASE HELP! :)</p>