Stefan Kendall
Stefan Kendall

Reputation: 67852

NSCollectionView - custom view item either isn't wired or won't select

Here's how I create the collection view item:

func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
    let item = collectionView.makeItemWithIdentifier("ViewItem", forIndexPath: indexPath)
    guard let collectionViewItem = item as? ViewItem else {
        return item
    }
    return collectionViewItem
}

If I don't register the custom class ViewItem, selection works! As expected, IBOutlets are nil and I can't modify the object.

Before reloadData in viewDidLoad, if I use self.collectionView.registerClass(ViewItem.self, forItemWithIdentifier: "ViewItem"), then IBOutlets are wired, and I can change the object, but no selection seems to work.

func collectionView(collectionView: NSCollectionView, didSelectItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
    print("Select")
}

Never gets called. ViewItem is a xib with base class NSCollectionViewItem. It has a single label (for now, to test this to get it working), and I've changed no default options.

What am I missing?

Upvotes: 2

Views: 638

Answers (1)

gaosiyang
gaosiyang

Reputation: 11

Try adding

collectionView.selectable = YES

Hope it works for you.

Upvotes: 1

Related Questions