NSCollectionView 的 collectionView:itemForRepresentedObjectAtIndexPath: 方法从未被调用

NSCollectionView's collectionView:itemForRepresentedObjectAtIndexPath: method is never called

我已经习惯了 iOS 的 UICollectionView 并且正在尝试在 OSX 上设置一个 NSCollectionView 而不使用任何故事板(大多数教程使用 .xibs ,所以它们略有不同)。 API 看起来很相似,但我无法获取集合视图来调用数据源的 collectionView:itemForRepresentedObjectAtIndexPath: 方法。

我已将原始代码简化为我认为让集合视图调用此 dataSource 方法所需的最少代码:

@import Cocoa;

static NSString * const kCollectionViewControllerItemID = @"ItemID";

@interface CollectionViewItem : NSCollectionViewItem
@end

@implementation CollectionViewItem
@end

@interface CollectionViewController : NSViewController  <NSCollectionViewDelegate, NSCollectionViewDataSource>

@property (nonatomic, strong) NSCollectionView *collectionView;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSCollectionViewFlowLayout *layout = [[NSCollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(100, 100)];

    _collectionView = [[NSCollectionView alloc] init];
    [_collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_collectionView setCollectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    [_collectionView registerClass:[CollectionViewItem class] forItemWithIdentifier:kCollectionViewControllerItemID];
    [[self view] addSubview:_collectionView];
    [[self view] addConstraints:@[
                                  [NSLayoutConstraint constraintWithItem:_collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[self view] attribute:NSLayoutAttributeTop multiplier:1.0f constant:0],
                                  [NSLayoutConstraint constraintWithItem:_collectionView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:[self view] attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0],
                                  [NSLayoutConstraint constraintWithItem:[self view] attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_collectionView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0],
                                  [NSLayoutConstraint constraintWithItem:[self view] attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_collectionView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0],
                                  ]];
}

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

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
    //this method is never called
    return [collectionView makeItemWithIdentifier:kCollectionViewControllerItemID forIndexPath:indexPath];
}

@end

此实现缺少什么?

编辑: 为了使其更容易重现,我在 GitHub https://github.com/BradB132/OSXCollectionViewTest

上设置了一个示例项目

所以事实证明您需要将 NSCollectionView 包装在 NSScrollView 中:

NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
_collectionView = [[NSCollectionView alloc] initWithFrame:NSZeroRect];
scrollView.documentView = _collectionView;
[self.view addSubview:scrollView];

collectionView:itemForRepresentedObjectAtIndexPath: 现在应该调用了。