2015-12-24 14 views
9

Có bộ não rắm và không thể tìm ra lý do tại sao các phương pháp dataSource cho NSCollectionView không được gọi.Cocoa NSCollectionView không gọi dữ liệu Phương thức mã nguồn

Tất cả các mẫu mà tôi sắp gặp cho NSCollectionView đang sử dụng các liên kết. Ngay cả "Hướng dẫn lập trình" của Apple là cực kỳ ngắn và mơ hồ so với hầu hết các hướng dẫn lập trình khác của họ thường là tuyệt vời.

Tôi có thiết lập thử nghiệm hộp cát chỉ để kiểm tra điều này. Tôi đang tải hình ảnh từ dribbble.com.

Dưới đây là AppDelegate.h tôi:

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

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

Và 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 

Tất cả mọi thứ ngoại trừ bộ sưu tập các phương pháp nguồn dữ liệu đang được gọi.

Tôi đã đột phá và bước qua mọi thứ và đảm bảo không có con trỏ nào.

Tôi đã kiểm tra và kiểm tra xem IBOutlet có không phải là không.

Lớp DribbbleCell hiện không làm gì vì tôi vẫn đang cố gắng tìm ra lý do tại sao các phương thức nguồn dữ liệu này không được gọi.

Tôi thiếu gì đối với NSCollectionView?

Trả lời

20

Tôi đã tìm ra. Hóa ra trong giao diện người dùng, bạn phải thay đổi bố cục, nó được đặt thành Mảng nội dung (kế thừa) theo mặc định.

enter image description here

+1

Cảm ơn cho điều này, đến NSCollectionView từ UICollectionView khá Gần đây tôi không hề biết có một điều như chế độ mảng nội dung. – Ash

+0

@Ash mảng nội dung được sử dụng trong quá khứ cho Ràng buộc dữ liệu –

+4

Tùy chọn mặc định này như thế nào? Tùy chọn mặc định không gọi phương thức nguồn dữ liệu? Thực sự, Apple ??? –

Các vấn đề liên quan