I lost a little here: I created a button acting like a colorPicker: clicking on it shows a collectionView in a popover. At first I did this with a nib file containing view + collectionView (built-in to scrollView + clipView). The material works great.
Since the nib file is very simple (and to improve my coding skills when developing representations programmatically), I decided to get rid of the nib file and write the missing part in the code. The thing is, I was able to do this job, except for the contents of the View collection. After deep research, it turns out that inside the method:
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem
which should manage the data source, method
collectionView.makeItem(withIdentifier: String, for: IndexPath)
does not work. In fact, in:
let item = collectionView.makeItem(withIdentifier: ColorPickerPopover.itemIdentifier, for: indexPath)
the element is not initialized, as the debugger says when I enter (not zero, uninitialized ). Apparently, the makeItem method never instantiates any collectionViewItem from the subclass I made. The identifier is beautiful, and the collectionView.register function is called, as in the nib version, since both projects are identical at these points. The makeItem function simply does not call the loadView method of the NSCollectionViewItem subclass.
Any clue?
Josh
swift macos nscollectionview
Joshua
source share