question about NSCollectionView

I am a beginner. I have this code in my ViewController and the issue at hand is the func on line 16. I want to be able to access "test" from my ColViewItem.swift from within this function. That way I can customize my items.

import Cocoa
class ViewController: NSViewController, NSCollectionViewDelegate, NSCollectionViewDataSource {

    @IBOutlet weak var colView: NSCollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let item = NSNib(nibNamed: "ColViewItem", bundle: nil)
        colView.register(item, forItemWithIdentifier: "ColViewItem")
        colView.delegate = self
        colView.dataSource = self
   
    }
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1;
    }
    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
   
        let item = collectionView .makeItem(withIdentifier: "ColViewItem", for: indexPath)
   
       
        return item
       
    }
   
}


Here is my ColViewItem.swift

import Cocoa
class ColViewItem: NSCollectionViewItem {
    @IBOutlet weak var test: NSImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
  
      
    }
  
}



Help.

Replies

You need to tell that item is of type ColViewItem


    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        var item = collectionView.makeItem(withIdentifier: "ColViewItem", for: indexPath) as! ColViewItem
        item.test = // your NSImageView to load

        return item
    }