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?
I figured it out. It turns out in interfacebuilder you have to change the layout, it's set to Content Array (legacy) by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With