Comparing NSCollectionView and UICollectionView #1
In porting apps from iOS to macOS, one would compare the similarities and port the code over. Since 2015 with OSX El Capitan 10.11 (now called macOS), NSCollectionView has been refined to modern APIs which looks much like the iOS siblings. Yet, there are still some subtle changes that you wouldn’t notice until working on it. I will write about these in the upcoming posts.
This time I am going to start with the core element of the content container. In macOS, it is called NSCollectionViewItem. In iOS, you will find it as UICollectionViewCell.
Nature of the content container
To point out the fundamentals of both containers, one shall start with their ancestors. Here is what the inheritance tree look like:
NSCollectionViewItem > NSViewController
UICollectionViewCell > UICollectionReusableView > UIView
In macOS environment, the container is a controller, which carries a bunch of lifecycle methods, like any other View Controller does. In iOS, however, it is a type of view, which can include subviews of elements.
View Hierarchy
To continue the previous point, NSCollectionViewItem does not have a view by default. One shall create the view programmatically or using Interface Builder. You can learn more about how to build the basics of this in my YouTube video.
In iOS, it is a lot simpler. Say you are building the content using Interface Builder, simply drag components to the view that comes with creating UICollectionViewCell.
Besides, UICollectionViewCell also comes with some default properties on accessing views.
contentView — Default View that comes with creation
backgroundView — (Optional) the view to display behind the cell’s other content
selectedBackgroundView — (Optional) the view that is displayed just above the background view when the cell is selected
In the next post, I will talk about the difference in preparing this content container.
References
I am taking a challenge to write a post every day for 180 days. The content will be related to my life as a father, entrepreneur, developer and trainer.