9

Having a brain fart and cannot figure out why the dataSource methods for NSCollectionView are not being called.

All the samples that I'm coming across for NSCollectionView are using bindings. Even Apple's "Programming Guide" is extremely short and vague compared to most of their other programming guides which are usually amazing.

I have a sandbox test setup just to test this. I'm loading images from dribbble.com.

Here's my AppDelegate.h:

#import <Cocoa/Cocoa.h>
#import "Dribbble.h"

@interface AppDelegate : NSObject <NSApplicationDelegate,NSCollectionViewDataSource,NSCollectionViewDelegate>
@property IBOutlet NSCollectionView * collectionView;
@end

And AppDelegate.m

#import "AppDelegate.h"
#import "DribbbleCell.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property Dribbble * dribbble;
@property NSMutableArray * dribbbleShots;
@property NSInteger page;
@property NSInteger maxPage;
@end

@implementation AppDelegate

- (void) applicationDidFinishLaunching:(NSNotification *) aNotification {
    self.page = 0;
    self.maxPage = 1;
    self.dribbbleShots = [NSMutableArray array];

    self.dribbble = [[Dribbble alloc] init];
    self.dribbble.accessToken = @"XXXX";
    self.dribbble.clientSecret = @"XXXX";
    self.dribbble.clientId = @"XXXX";

    NSNib * nib = [[NSNib alloc] initWithNibNamed:@"DribbbleCell" bundle:nil];
    [self.collectionView registerNib:nib forItemWithIdentifier:@"DribbbleCell"];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    [self loadDribbbleShots];
}

- (void) loadDribbbleShots {
    self.page++;

    //this loads 100 shots up to max pages.
    [self.dribbble listShotsWithParameters:@{@"per_page":@"100",@"page":[NSString stringWithFormat:@"%lu",self.page]} completion:^(DribbbleResponse *response) {
        [self.dribbbleShots addObjectsFromArray:response.data];
        if(self.page < self.maxPage) {
            [self performSelectorOnMainThread:@selector(loadDribbbleShots) withObject:nil waitUntilDone:FALSE];
        } else {
            [self finishedDribbbleLoad];
        }
    }];
}

- (void) finishedDribbbleLoad {
    //how do I reload collection view data?
    [self.collectionView setContent:self.dribbbleShots];
    [self.collectionView reloadData];
    [self.collectionView setNeedsDisplay:TRUE];
}

//** None of the below methods are being called.

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

- (NSInteger) collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dribbbleShots.count;
}

- (NSCollectionViewItem * ) collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
    DribbbleCell * cell =  (DribbbleCell *)[collectionView makeItemWithIdentifier:@"DribbbleCell" forIndexPath:indexPath];
    return cell;
}

@end

Everything except for the collection data source methods are being called.

I've break-pointed and stepped through everything and made sure there's no nil pointers.

I've checked and checked that the IBOutlet is not nil.

The DribbbleCell class currently does nothing as i'm still trying to figure out why these data source methods aren't being called.

What am I missing for NSCollectionView?

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56

1 Answers1

23

I figured it out. It turns out in interfacebuilder you have to change the layout, it's set to Content Array (legacy) by default.

enter image description here

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56
  • 1
    Thanks for this, coming to NSCollectionView from UICollectionView fairly recently I didn't know there was such a thing as content array mode. – Ash Jan 13 '16 at 11:14
  • @Ash content array was used in the past for Data Binding – Harry Ng Feb 19 '16 at 03:52
  • 6
    How is this the default option? The default option doesn't call data source methods? Really, Apple??? – Adam Johns May 16 '16 at 19:47