4

I am trying to add a header to a collection view. I am using a custom layout that scrolls horizontally, it is used to view a list of avatar images for friends. I can get the header to appear but it does NOT dequeue. As soon as the header view goes off screen, its gone for good. Can anyone figure out why this is?

Thank you!

Collection View data source:

- (UICollectionReusableView *)collectionView:(SWAvatarViewerCollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{

    if (self.showAddAvatarHeaderView && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
        return [collectionView dequeueAddAvatarViewHeaderForIndexPath:indexPath];
    }

    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(SWAvatarViewerCollectionViewFlowLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (!self.showAddAvatarHeaderView) {
        return CGSizeZero;
    }

    return CGSizeMake(kSWAvatarViewerAddAvatarHeaderViewWidth, CGRectGetHeight(collectionView.bounds));
}

Avatar collection view:

- (SWAvatarViewerAddAvatarHeaderView *)dequeueAddAvatarViewHeaderForIndexPath:(NSIndexPath *)indexPath {
    SWAvatarViewerAddAvatarHeaderView *headerView = [super dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                             withReuseIdentifier:[SWAvatarViewerAddAvatarHeaderView headerReuseIdentifier]
                                                                                    forIndexPath:indexPath];
    headerView.delegate = self;
    return headerView;
}

Nib file registration:

  [self registerNib:[SWAvatarViewerAddAvatarHeaderView nib]
        forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
        withReuseIdentifier:[SWAvatarViewerAddAvatarHeaderView headerReuseIdentifier]];

Layout:

#pragma mark - Initialization

- (void)configureFlowLayout {
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // Padding for cells is taken into account in the cell's layout. Remove all
    // padding between cells
    self.sectionInset = UIEdgeInsetsMake(0, 00.0f, 0, 00.0f);
    self.minimumLineSpacing = 0.0f;
    self.minimumInteritemSpacing = CGFLOAT_MAX;

    _cellBottomLabelFont = [UIFont systemFontOfSize:12.0];

    CGSize defaultAvatarSize = CGSizeMake(44.0f, 44.0f);
    _avatarViewSize = defaultAvatarSize;

    _springinessEnabled = YES;
    _springResistanceFactor = 1000;
}
DBoyer
  • 3,012
  • 4
  • 20
  • 32
  • This has nothing to do with "dequeue". If the header view appears, it dequeues (or at least it appears). If it vanishes after it has scrolled offscreen, that has to do with the NSCollectionViewLayout. But you don't say anything about what that is, so you haven't given any of the relevant information. – matt Aug 02 '14 at 22:50
  • So it's _not_ a custom layout after all? It's just a normal built-in flow layout? – matt Aug 02 '14 at 23:34
  • Its just a tweaked flow layout with some UIKitDynamics to add spring between cells, its essentially a horizontal flow layout though. – DBoyer Aug 02 '14 at 23:40
  • Are you overriding prepareLayout in your custom layout? If not, you might want to do so and make sure that the your header view has the correct frame. – Lev Landau Aug 08 '14 at 09:49

3 Answers3

1

You're apparently using a third-party layout I've never heard of. After some discussion with you, my feeling is that my first comment under your question was probably right: the layout itself may be buggy.

In a collection view, the layout attributes of the cells (position, size, transform, alpha, etc.) are the responsibility of the layout. So if something disappears merely because it is scrolled off the screen and then back on, it sounds like the layout itself is not doing its job correctly.

matt
  • 447,615
  • 74
  • 748
  • 977
  • This is a library that I wrote. The layout and all its bugs are ones I made :P – DBoyer Aug 05 '14 at 07:39
  • 2
    Then you have asked completely the wrong question. And you still have not shown your layout code, as I asked you to, two days ago. You are not supplying enough information for anyone to help you. – matt Aug 05 '14 at 12:39
1

Quick googleing did not unveil the SWAvatarViewerCollectionViewFlowLayout. If you have the sources, you can take a look at the layout code, there should be a method called layoutAttributesForElementsInRect:.

The collection view is dequeuing items as soon as they go offscreen which is determined with the help of the aforementioned method (The layout attributes contain the views center and dimensions). If the method returns always the layout attributes for the header it will not get dequeued.

However if you do not have access to the code (like a static lib) you probably can not do much about it.

TheNiftyCoder
  • 2,687
  • 2
  • 22
  • 40
  • I wrote the layout myself. I will check out the layoutAttributesForElementsInRect: method again. Thanks – DBoyer Aug 05 '14 at 07:41
1

Just put this method that will solve all the problems

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(-50, 10, 10, 10); //asuming

    //UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
} 

Enjoy.

Bhavesh Lathigara
  • 1,297
  • 1
  • 14
  • 23