Kodeco Forums

NSCollectionView Tutorial

In this NSCollectionView tutorial for macOS, you'll learn how to add a collection view to your app and populate it with a data source - using Swift!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/783-nscollectionview-tutorial

Out of interest: have you managed to get NSCollectionView work solely with Storyboards rather than using a .xib file for the collectionViewItem?

It doesn’t seem possible to make an XIB for the NSCollectionViewItem anymore, so I put it in the storyboard. Can’t figure out how to connect it to the CollectionView. Any ideas?

I build the XIB manually from an empty XIB and got it to work. Would be still nice to know how to get it connected with StoryBoard.

When one uses the elasticity feature of the scrollView, the background color shown in the “elastic area” is white. Here’s how to fix that:

  1. In Interface Builder, select the CollectionView in the document outline.
  2. In the Utilities window, show the Attributes inspector.
  3. Set the desired color in the Primary color selector control of the Attributes inspector.
  4. Remove code that sets the CollectionView background color.

hello, I want to make a nsCollectionView What it looks like in Finder, But I can’t figure it out. How can I do that?

I’m converting an application that has a CollectionView from Objective C to Swift 4.0. I have much of it converted but I’ve run into a problem with displaying a subview then selecting a button in the CollectionViewItem class. I defined my collection view on the main storyboard and created a separate class (CollectionViewItem) and xib for the collection view item (as done in Gabe’s tut on CollectionView).

I then did the same for the subview (ShowSongInfo).

I binded the info button in the CollectionViewItem View to the CollectionViewItem’s class’s IBAction:

@IBAction func songInfoPressed(_ sender: Any) {
var name = " "
if let song = song {
name = song.fileName
}

    let subview = ShowSongInfo(nibName: NSNib.Name(rawValue: "ShowSongInfo"), bundle: nil)
    self.view.addSubview(subview.view)
    
    print(name)
}

In the ShowSongInfo Class, in the ViewDidLoad method I put a print statement to verify the Class was being called:

override func viewDidLoad() {
super.viewDidLoad()
print(“SongInfoDidLoad”)
// Do view setup here.
}

Both print statements appear in the log; no errors are recorded but the ShowSongInfo subview never displays.

I’m not sure what I’m attempting is doable. In the Objective C App I used a sheet to display the songs tag info. Here I would love the sheet effect or possibly a popover, but right now I’d love for the ShowSongInfo window to simply appear. Any help or suggestions would be appreciated.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]