NSCollectionView的collectionView:itemForRepresentedObjectAtIndexPath:方法永远不会被调用

时间:2016-05-21 04:38:47

标签: objective-c macos nscollectionview

我已经习惯了iOS的UICollectionView,并且我试图在不使用任何故事板的情况下在OSX上设置NSCollectionView(大多数教程都使用.xibs,因此它们略有不同)。 API看起来很相似,但是我无法让集合视图调用dataSource的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

上设置了一个示例项目

1 个答案:

答案 0 :(得分:0)

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

AppTenant

应立即调用collectionView:itemForRepresentedObjectAtIndexPath: