In recent days, I have been working on a different scenario, and I dare say that using NSScrollView, or not, makes almost no difference. With or without scrollView, I encountered the same errors and limitations.
What is of great importance is the choice between the "old school" and the newfangled element of the collection. By “old school,” I mean setting the properties of the ItemPrototype object and content, something like this:
NSCollectionView *collectionView = [[NSCollectionView alloc] init]; collectionView.itemPrototype = [TBCollectionViewItem new]; collectionView.content = self.collectionItems; NSInteger index = 0; for (NSString *title in _collectionItems) { NSIndexPath *path = [NSIndexPath indexPathForItem:index inSection:0]; TBCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"Test" forIndexPath:path]; item.representedObject = title; index++; }
New school, something like that:
NSCollectionView *collectionView = [[NSCollectionView alloc] init]; collectionView.identifier = TBCollectionViewIdentifier; [collectionView registerClass:[TBCollectionViewItem class] forItemWithIdentifier:TBCollectionViewItemIdentifier];
Now you may have noticed the comment that registerClass: must be called before makeItemWithIdentifier: forIndexPath. In practice, this means calling registerClass: before installing .dataSource, while in your code you first install .dataSource. Status of documents:
Although you can register new elements at any time, you should not call the makeItemWithIdentifier: forIndexPath: method until you register the corresponding element.
I would like to say that by switching these two lines, all layout problems will be solved. Unfortunately, I found that the combination .collectionViewLayout / .dataSource is a layout catastrophe recipe. Regardless of whether this can be fixed by switching from NSCollectionViewGridLayout to flowLayout, I'm still not sure.
Elise van looij
source share