objective-cmacosmacos-sierranscollectionviewnscollectionviewitem

NSCollectionViewItems don't reload when scrolling <OSX - Objective C>


I'm new to learning Objective-C and I have a problem with NSCollectionView which I couldn't find any ways to resolve it.

My problem is:

I wrote a button event to create a NSCollectionView named thoughtCollectionView and added onto my contentView to display NSCollectionViewItems.

When I run the simulator, items look okay, but they don't reload when I scroll.

Here's my code:

MainViewController.m

- (void)thoughtShow{

    // Add Collection View
    thoughtCollectionView = [[ThoughtCollection alloc] initWithFrame:NSMakeRect(0, 0, contentWidth, contentHeight)];
    NSCollectionViewFlowLayout *layout = [[NSCollectionViewFlowLayout alloc] init];

    [thoughtCollectionView setCollectionViewLayout:layout];
    [layout setScrollDirection:NSCollectionViewScrollDirectionVertical];
    layout.itemSize = NSMakeSize(50, 50);
    layout.minimumInteritemSpacing = 20;
    layout.sectionInset = NSEdgeInsetsMake(20, 20, 20, 20);

    [thoughtCollectionView registerClass:ThoughtItems.self forItemWithIdentifier:@"ThoughtItems"];
    thoughtScrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, contentWidth, contentHeight)];
    thoughtScrollView.documentView = thoughtCollectionView;
    [contentView addSubview:thoughtScrollView];

    [thoughtCollectionView reloadData];
    [thoughtCollectionView setNeedsDisplay:YES];
    [thoughtCollectionView layoutSubtreeIfNeeded];
}

MainViewController.h

@interface MainViewController : NSViewController <NSCollectionViewDelegateFlowLayout, NSCollectionViewDelegate, NSCollectionViewDataSource>
@end

ThoughtCollectionView.h

@interface ThoughtCollection : NSCollectionView <NSCollectionViewDataSource, NSCollectionViewDelegate, NSCollectionViewDelegateFlowLayout>
{
    NSMutableArray * ar;
}

@end

ThoughtCollectionView.m

@implementation ThoughtCollection

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath{
    ThoughtItems *item = [collectionView makeItemWithIdentifier:@"ThoughtItems" forIndexPath:indexPath];
    NSLog(@"Reloading item");
    return item;
}

- (void)collectionView:(NSCollectionView *)collectionView willDisplayItem:(nonnull NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath{
}

- (void)collectionView:(NSCollectionView *)collectionView didEndDisplayingItem:(nonnull NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath{
}

- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"Collection view count : %lu", [ar count]);
    return [ar count];
}

- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView{
    return 1;
}

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

- (void)viewDidMoveToWindow {

    NSLog(@"My collection View");
    self.delegate = self;
    self.dataSource = self;
    ar = [[NSMutableArray alloc] init];

    for (int n = 0; n < 1000; n++) {
        [ar addObject:@"Hello"];
    }

    [self reloadData];
    [self setNeedsDisplay:YES];
    [self layoutSubtreeIfNeeded];
}
@end

Simulator display: enter image description here

Scroll: enter image description here

ThoughtItem.m

#import "ThoughtItems.h"

@interface ThoughtItems ()

@end

@implementation ThoughtItems

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setWantsLayer:YES];
    [self.view.layer setBackgroundColor:[[NSColor orangeColor] CGColor]];
}

@end

ThoughtItem.h

#import <Cocoa/Cocoa.h>

@interface ThoughtItems : NSCollectionViewItem

@end

Solution

  • I found out that what's the problem with the CollectionItem. I have to add an Object in the ThoughtItem.xib file, then change the object class name to "ThoughtItem".

    And I change -(void)viewDidMoveToWindow to a method which I can call it from MainViewController. (or create NSCollectionView in the MainViewController is a way to solve it too)

    Then all the problems are solved!