Custom NSCollectionViewLayout is not displaying anything

127 Views Asked by At

I'm trying to learn how to create a custom NSCollectionViewLayout. I've followed some tutorials, but I always got the same result, an empty CollectionView.

The code that I've been using is this one: https://github.com/Jatapiaro/CustomCollectionViewLayout

My implementation of the custom NSCollectionViewLayout is:

#import "CollectionViewCustomLayout.h"

@implementation CollectionViewCustomLayout {
    CGFloat _itemHeight;
}

- (instancetype)init
{
    if (self = [super init]) {
        _itemHeight = 100;
    }

    return self;
}

- (void)prepareLayout
{
    [super prepareLayout];
}

/**
 * The width and height of the entire collection view content
 */
- (NSSize)collectionViewContentSize
{
    NSInteger numberOfItems = self.collectionView.numberOfSections;

    if (!numberOfItems)
        return NSZeroSize;

    NSSize size = self.collectionView.superview.bounds.size;
    size.height = _itemHeight * numberOfItems;

    return size;
}

/**
 * Set the layout of each item inside the collection view
 */
- (NSArray<__kindof NSCollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(NSRect)rect
{
    NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
    NSMutableArray<NSCollectionViewLayoutAttributes *> *attributes = [NSMutableArray array];

    for (NSInteger i = 0; i < numberOfItems; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        NSCollectionViewLayoutAttributes *itemAttributes = [NSCollectionViewLayoutAttributes layoutAttributesForItemWithIndexPath:indexPath];

        if (itemAttributes)
            [attributes addObject:itemAttributes];
    }

    return attributes;
}

- (NSCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];

    if (!numberOfItems)
        return nil;

    NSRect frame = NSMakeRect(0, _itemHeight * indexPath.item, self.collectionViewContentSize.width, _itemHeight);

    NSCollectionViewLayoutAttributes *itemAttributes = [NSCollectionViewLayoutAttributes layoutAttributesForItemWithIndexPath:indexPath];
    itemAttributes.frame = frame;

    return itemAttributes;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(NSRect)newBounds
{
    return YES;
}

@end

Even that all the methods in the implementation are called, I always get the following result once the app is running.

CollectionView failure

Does anyone has an idea of what I'm doing wrong?

0

There are 0 best solutions below