2015-12-24 4 views
9

Mit einem Gehirn furzen und kann nicht herausfinden, warum die DataSource-Methoden für NSCollectionView nicht aufgerufen werden.Cocoa NSCollectionView nicht aufrufen dataSource Methoden

Alle Samples, die ich für NSCollectionView finde, verwenden Bindings. Selbst Apples "Programming Guide" ist extrem kurz und vage im Vergleich zu den meisten anderen Programmierhilfen, die normalerweise erstaunlich sind.

Ich habe ein Sandbox-Test-Setup nur um dies zu testen. Ich lade Bilder von dribbble.com.

Hier ist meine AppDelegate.h:

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

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

Und 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 

Alles außer für die Sammlung Datenquelle Methoden aufgerufen werden.

Ich habe durch alle Punkte gebrochen und durchgetreten und dafür gesorgt, dass es keine Nullzeiger gibt.

Ich habe überprüft und überprüft, dass das IBOutlet nicht Null ist.

Die DribbleCell-Klasse führt derzeit nichts aus, da ich immer noch versuche herauszufinden, warum diese Datenquellenmethoden nicht aufgerufen werden.

Was fehlt mir für NSCollectionView?

Antwort

20

Ich fand es heraus. Es stellt sich heraus, dass Sie in interface-builder das Layout ändern müssen, standardmäßig ist es auf Content-Array (Legacy) gesetzt.

enter image description here

+1

Vielen Dank für diese, zu NSCollectionView von UICollectionView kommt nicht allzu langer Zeit wusste ich nicht, so etwas wie Content-Array-Modus war. – Ash

+0

@Ash Inhalt Array wurde in der Vergangenheit für Datenbindung verwendet –

+4

Wie ist das die Standardoption? Die Standardoption ruft keine Datenquellenmethoden auf? Wirklich, Apple ??? –