0

I have trouble accessing the stringValue of a textLabel in a xib-file, that I use for my generic NSCollectionViewItem.

Registering collectionView from xib-file:

override func viewDidLoad() {
    super.viewDidLoad()

    let item = NSNib(nibNamed: NSNib.Name("collectionViewItem"), bundle: nil)

    collectionView.register(item, forItemWithIdentifier: NSUserInterfaceItemIdentifier("collectionViewItem"))

    collectionView.delegate = self
    collectionView.dataSource = self
}

Making NSCollectionViewItem:

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {

    let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier("collectionViewItem"), for: indexPath)

    // Here I want to access the stringValue of the textLabel within my collectionViewItem. 

    return item
}

Any ideas as to how I could create a reference to this textLabel and manipulating it?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • "Registering collectionView from xib-file" Que!? What for? Keep further working on your itemForRepresentedObjectAt delegate method. – El Tomato Mar 11 '18 at 01:15

1 Answers1

0

Make a custom class MyCollectionViewItem that overrides NSCollectionViewItem. Add a textLabel member and link it up in your nib file. Then cast when using makeItem:

let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier("collectionViewItem"), for: indexPath) as! MyCollectionViewItem
Giles
  • 1,428
  • 11
  • 21