10

I'm creating a basic collection view layout which subclasses UICollectionViewFlowLayout. However, I'm noticing that there appear to be several decoration views stacked on top of one another.

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.

[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];
}];

I've corroborated this by giving my decoration views an alpha of 0.2f and seeing them stack up.

enter image description here

I also performed a dump of the collection view hierarchy and saw 10 instances of AFDecorationView when I should only see 4:

   | <AFDecorationView: 0x719ee50; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); alpha = 0; hidden = YES; layer = <CALayer: 0x719eec0>>
   | <AFDecorationView: 0x71ad980; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); alpha = 0; hidden = YES; layer = <CALayer: 0x71adb60>>
   | <AFDecorationView: 0x71afc90; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); layer = <CALayer: 0x71afd60>>
   | <AFDecorationView: 0xd79ac30; baseClass = UICollectionReusableView; frame = (0 596.406; 768 203.281); alpha = 0; hidden = YES; layer = <CALayer: 0xd79ad00>>
   | <AFDecorationView: 0xd79cf20; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); layer = <CALayer: 0xd79cff0>>
   | <AFDecorationView: 0xd79dac0; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); layer = <CALayer: 0xd79a980>>
   | <AFDecorationView: 0xd794fd0; baseClass = UICollectionReusableView; frame = (0 859.688; 768 225.938); layer = <CALayer: 0xd7950a0>>
   | <AFDecorationView: 0xd7a1300; baseClass = UICollectionReusableView; frame = (0 596.406; 768 203.281); layer = <CALayer: 0xd7a13d0>></CALayer:>
   | <AFDecorationView: 0xd7a35d0; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); layer = <CALayer: 0xd794470>>
   | <AFDecorationView: 0xd7a43e0; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); layer = <CALayer: 0xd7a44b0>>

I have tried looking at another custom layout example, but they seem to instantiate quite a few instances of their decoration views, too. Could this be a bug in UICollecionView? Or is it up to us to only add the decoration view layout attribute in layoutAttributesForElementsInRect: once per section?

The decoration-related parts of custom layout look like the following.

@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 < numberOfCellsInSection; i++)
        {
            NSIndexPath *cellIndexPath = [NSIndexPath indexPathForItem:i inSection:indexPath.section];

            UICollectionViewLayoutAttributes *cellAttribtes = [self layoutAttributesForItemAtIndexPath:cellIndexPath];

            if (CGRectGetHeight(cellAttribtes.frame) > 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

I only want one decoration view per section, so I'm adding the decoration view to the first section.

6
  • I'm still thinking about this, but I have 2 comments: (a) can you check and see how many times your if (attributes.indexPath.item == 0) is true? Perhaps there are other views, like other supplementary views which are causing it to fire more than you expect? and (b) it seems a bit funny to use the presence of the row 0 cell in a given section to determine if you should include your decoration cell, since they (I assume) occupy different rects and so some calls to layoutAttributesForElementsInRect: should probably return one and not the other. Jan 13, 2013 at 20:04
  • Also: does the behaviour change if you just use reloadData instead of doing your batch updating? Jan 13, 2013 at 20:07
  • A while ago I struggled with a collection view problem too. After a lot of hours trying to fix it, I submitted a TSI. The Apple engineer told me that it was a bug. It seems to be quite buggy… Jan 13, 2013 at 20:12
  • @JesseRusak Thanks for the suggestions - I will experiment to see if you're right. Can you suggest an alternative for for indexPath.item == 0? My decoration views are one per section. Thanks!
    – Ash Furrow
    Jan 13, 2013 at 23:14
  • I've changed my check to attributes.representedElementCategory == UICollectionElementCategorySupplementaryView and added logging to see if it's being added more than once, but it looks normal. I think GuidoH is right - this is just buggy.
    – Ash Furrow
    Jan 13, 2013 at 23:34

2 Answers 2

3

This appears to be a bug in UICollectionView. I've filed a bug report with Apple.

5
  • Yep, I think you're right. I have a similar issue, in which decoration views of the wrong kind are being used after a content update, and existing ones not being removed. This isn't the only bug I reckon I've seen in UICollectionView either. Hopefully it will be sorted in the next release.
    – Ash
    Feb 27, 2013 at 21:10
  • Having similar issues. UICollectionView is not production ready IMHO. Too many bugs, especially with decoration/supplementary views. Apple really dropped the ball with this one
    – cidered
    May 3, 2013 at 15:36
  • 3
    Did you get a reply from apple about the bug? I have a similar issue with supplementary views stacking up - this time when i invalidateLayout. The extra views are never removed and this is causing real problems when my layout moves things around.
    – SimonB
    May 22, 2013 at 10:08
  • This has been resolved as of iOS[redacted] – no other recourse for now.
    – Ash Furrow
    Sep 9, 2013 at 20:10
  • I'm experiencing this bug on iOS 8.1.3. In my case it happens if I set the contentOffset programmatically. Feb 1, 2015 at 21:16
1

I had a similar issue but I was able to solve it by manually removing all decoration views in prepareLayout in my custom layout class (I do that right before I invoke [super prepareLayout]):

    // Iterate over all subviews to remove all decoration views 
    for (UIView *view in self.collectionView.subviews) 
    {
        if ([view isKindOfClass:[DecorationView class]])
        {
            [view removeFromSuperview];
        }

    }
    [super prepareLayout];

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.