DEV Community

Khoa Pham
Khoa Pham

Posted on

2

Making NSCollectionView programatically in Swift

Original post https://github.com/onmyway133/blog/issues/131

Here's how to create NSCollectionView programatically. We need to embed it inside NScrollView for scrolling to work. Code is in Swift 4

NSCollectionView

let layout = NSCollectionViewFlowLayout()
layout.minimumLineSpacing = 4

collectionView = NSCollectionView()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.collectionViewLayout = layout
collectionView.allowsMultipleSelection = false
collectionView.backgroundColors = [.clear]
collectionView.isSelectable = true
collectionView.register(
  Cell.self,
  forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell")
)
Enter fullscreen mode Exit fullscreen mode

NScrollView

scrollView = NSScrollView()
scrollView.documentView = collectionView
view.addSubview(scrollView)
Enter fullscreen mode Exit fullscreen mode

NSCollectionViewItem

final class Cell: NSCollectionViewItem {
  let label = Label()
  let myImageView = NSImageView()

  override func loadView() {
    self.view = NSView()
    self.view.wantsLayer = true
  }
}
Enter fullscreen mode Exit fullscreen mode

NSCollectionViewDataSource

func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  return coins.count
}

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  let cell = collectionView.makeItem(
    withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"),
    for: indexPath
  ) as! Cell

  let coin = coins[indexPath.item]

  cell.label.stringValue = coin.name
  cell.coinImageView.image =
    NSImage(named: NSImage.Name(rawValue: "USD"))
    ?? NSImage(named: NSImage.Name(rawValue: "Others"))

  return cell
}
Enter fullscreen mode Exit fullscreen mode

NSCollectionViewDelegateFlowLayout

func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
    guard let indexPath = indexPaths.first,
    let cell = collectionView.item(at: indexPath) as? Cell else {
      return
  }
}

func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  guard let indexPath = indexPaths.first,
    let cell = collectionView.item(at: indexPath) as? Cell else {
      return
  }
}

func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {

  return NSSize(
    width: collectionView.frame.size.width,
    height: 40
  )
}
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
devulderjeanpaul profile image
DevulderJeanPaul

on example
coinImageView doesn't exist remplace with myImageView

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs