Context:
I have already done the following
1. Watched the relevant videos for UICollectionView from WWDC 2012. Unfortunately they do not discuss much about the decoration views.
2. Implemented a collection view with decoration view. I subclassed the UICollectionViewFlowLayout to include the decoration view.
3. I have read the UICollectionView programming guide from Apple.
4. I understand that the Decoration views are controlled solely by the Layout object.
Problem:
1. When I analyze the code with Instruments I find that my decoration views are causing a memory leak.
2. On further analysis of the code I found that the decoration view is not being reused as I would expect. Every time the decoration view is needed a new one is created.
3. When the collection view deallocs only the last created decoration view gets dellocated and all other decorations views leak.
4. I do not understand how the decoration view will be reused when there is no deque method for it.
Questions:
1. Do we have to manually manage the removal and addition of decoration views from the collection view?
I am confused as to how is the decoration view works in general. Any pointers for this would help. The code for my layout object is as below. In the code I am simply putting a 10 points wide bar at the top of my collection view. Everytime the bar is scrolled off the screen and then brought back into the screen, a new decoration view is allocated. When the collection view is deallocated only the last allocated decoration view gets deallocated. Rest all leak.
#import "CollectionViewFlowLayoutSubclass.h"
#import "CollectionViewDecorationView.h"
#define DECORATION_VIEW_KIND @"DecorationViewShelf"
@implementation CollectionViewFlowLayoutSubclass
- (void)awakeFromNib
{
[super awakeFromNib];
[self registerClass:[CollectionViewDecorationView class] forDecorationViewOfKind:DECORATION_VIEW_KIND];
}
- (void)prepareLayout
{
[super prepareLayout];
}
- (CGSize)collectionViewContentSize
{
return [super collectionViewContentSize];
}
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* larrayAttributes = [super layoutAttributesForElementsInRect:rect];
NSMutableArray* larrayMutableAttributes = [larrayAttributes mutableCopy];
CGRect lrectFrame = CGRectMake(0, 0, 320, 10);
if (CGRectIntersectsRect(rect, lrectFrame))
{
UICollectionViewLayoutAttributes* lobjLayoutAttributes =
[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:DECORATION_VIEW_KIND
withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
lobjLayoutAttributes.frame = CGRectMake(0, 0, 320, 10);
[larrayMutableAttributes addObject:lobjLayoutAttributes];
}
else
{
NSLog(@"Rect %@ does not intersect %@", NSStringFromCGRect(rect), NSStringFromCGRect(lrectFrame));
}
return [NSArray arrayWithArray:larrayMutableAttributes];
}
@end