以编程方式使用数据源创建NSCollectionView [英] Creating NSCollectionView with datasource programmatically

查看:83
本文介绍了以编程方式使用数据源创建NSCollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NSCollectionViewDataSource 以编程方式创建 NSCollectionView

I am trying to create a NSCollectionView programmatically using a NSCollectionViewDataSource.

代码非常简单:

self.collectionView = [[NSCollectionView alloc] init];
// Add collection view to self.view etc. 
self.collectionView.dataSource = self;
[self.collectionView registerClass:[NSCollectionViewItem class] forItemWithIdentifier:@"test"]
self.collectionView.collectionViewLayout = gridLayout;
[self.collectionView reloadData]

这导致以下方法被调用(如果我不要显式设置 collectionViewLayout 属性,这两个也不会被调用):

This leads to the following methods getting called (if I don't set the collectionViewLayout property explicitly these two don't get called either):

- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView*)collectionView
- (NSInteger)collectionView:(NSCollectionView*)collectionView numberOfItemsInSection:(NSInteger)section

但是,从未调用 collectionView:itemForRepresentedObjectAtIndexPath:。为了确保调用最后一个数据源方法,我还需要做其他事情吗?我已经确保两个计数调用返回> 0,所以这不是问题。

However, collectionView:itemForRepresentedObjectAtIndexPath: is never called. Is there something else that I need to do in order to make sure that the last data source method is called? I have made sure that the two count calls return > 0, so that's not the problem.

推荐答案

所以看来问题出在实际上是我没有将NSCollectionView包装在NSScrollView中。如果没有将其包装在滚动视图中,则可能与布局不正确有关(因此,不从数据源请求项目)。

So it seems that the problem was actually that I wasn't wrapping the NSCollectionView in a NSScrollView. This probably has to do with the layout being done incorrectly (so the items aren't requested from the data source) if it is not wrapped in a scroll view.

这篇关于以编程方式使用数据源创建NSCollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆