I am writing a very simple macOS application that I'd like to show a few images in a collection view. I don't need any special behavior for how they are displayed. The docs for NSCollectionViewItem say:
The default implementation of this class supports the creation of a simple item that displays a single image or string.
That is what I want. However, I can't find any information on how to create a default NSCollectionViewItem
.
The documentation for NSCollectionView states:
Every data source object is required to implement the following methods:
collectionView:numberOfItemsInSection:
collectionView:itemForRepresentedObjectAtIndexPath:
The second method above returns an NSCollectionViewItem
. From reading examples I gather that the traditional way of creating an NSCollectionViewItem
in this case is to call:
NSCollectionViewItem* newCollectionViewItem = [imageCollectionView makeItemWithIdentifier:<some identifier>
forIndexPath:indexPath];
The problem is that I don't understand what <some identifier>
should be. I don't have a nib that contains an NSCollectionViewItem
because I'm not subclassing it or customizing it in any way. I've tried adding the following to my data source:
- (void)awakeFromNib
{
[imageCollectionView registerClass:[NSCollectionViewItem class]
forItemWithIdentifier:@"Image"];
}
where imageCollectionView
is the NSCollectionView
in question. And then in my - (NSCollectionViewItem *)collectionView:itemForRepresentedObjectAtIndexPath:
method, I call:
NSCollectionViewItem* newCollectionViewItem = [imageCollectionView makeItemWithIdentifier:@"Image"
forIndexPath:indexPath];
But this throws an exception and prints this to the console:
2016-12-19 17:51:27.463 MyApp[28177:3926764] -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: NSCollectionViewItem in bundle (null).
followed by a stack trace.
So how do I go about creating and using an NSCollectionViewItem
that isn't subclassed or modified in any way?
Here is a very simple example which uses a Nib for the item's prototype:
The NIB contains just a
NSCollectionViewItem
with a text field as view.Addendum: I think you should create a
NSCollectionViewItem.xib
for the registerClass variant. A view controller will search for a NIB with its class name, if you doesn't create its view manually inloadView
. Thus, you can't use plainNSCollectionViewItem
without a NIB for registering a class, because ofmakeItemWithIdentifier:forIndexPath:
will access the view of the item.