Question

I have an NSCollectionView up and running fairly nicely with an NSArrayController. My collection view items are configured to be multi-selectable and I am happy with the it. However, when a mouse drag event occurs within the collection view, a selection rectangle is drawn. How do I disable this selection rectangle drawing and still keep multiple selection in my NSCollectionView? Thanks for any help!

Was it helpful?

Solution

NSCollectionView is a fairly opaque class, there are not too many ways you can easily modify its fundamental behaviour.

The drag actions appear to be handled by the private method _performDragFromMouseDown: rather than by overriding mouseDragged:. Exactly what the private method does I'm not sure.

The simple answer is that you can't change the selection appearance except by modifying private methods of NSCollectionView.

You may need to use a custom view instead of NSCollectionView if you must modify this behaviour.

OTHER TIPS

With the latest NSCollectionView (released 2015 I think?) it calls viewForSupplementaryElementOfKind on the delegate to get the view for drawing the selection rectangle.

You can "catch" this situation by comparing kind.rawValue to "NSCollectionElementKindSelectionRectIndicator" in the delegate, and return NSView() if you don't want it to draw.

If you do want to draw it, return collectionView.makeSupplementaryView(ofKind: using a kind/identifier made manually using the rawValue of "NSCollectionElementKindSelectionRectIndicator". These kinds/identifiers have been missing from the enums since 2015 I believe, and cause me grief everytime I implement NSCollectionView.

Edit: I looked up an old post I made about this on the Apple Developer Forums, and I mentioned that I had to guard against numberOfSections == 0, and return NSView() in that case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top