I have an NSCollectionView that is properly configured to display Core Data Entities based on what's found in an NSArrayController. These Core Data entities, named MyManagedType, are defined to include an int32 indexing number "indexNumber" to keep them ordered, and binary NSData *myData. I can also select the items by clicking them, but I can't "click and drag" them. I only need to be able to select 1 item at a time (preferable actually).
I populate the NSArrayController by dragging and dropping files from finder in to the NSCollectionView. It accepts dropped files by getting their FILENAMES. I then open the file, do my processing, populate myData, get a new indexNumber (indexNumber = # of MyManagedType entities in persistent store), and insert this NSManagedObject * in to my persistent store. That works great.
The 2 things I want to add, I'm lost.
1) I want to be able to click and grab an item in the NSCollectionView, drag it outside the view (or window), and drop it to "poof" it and have it delete from my persistent store.
2) I want to be able to click and grab an item in the NSCollectionView, drag it to ANOTHER SPOT in my NSCollectionView, and drop it to reorder my indexNumbers.
The main stumbling point here is I don't understand what drag&drop types to set myself up to handle - with my existing code it is filenames, but what is it now? I've read the class reference several times and I understand what a lot of the methods do, but I just can't wrap my head around how I achieve #1 and #2.
For the collection view to use NSCollectionViewDelegateFlowLayout, you must connect ViewController to the delegate outlet of NSCollectionView. Open Main.storyboard and select the collection view. Open the Connections Inspector, and locate the delegate in the Outlets section.
When an item is outside the collection view’s visibleRect the collection view isn’t maintaining an NSCollectionViewItem instance for this item. If this is the case than the collection view’s item (at:) method will return nil.
Finally, you need to disable any Drag and Drop logic defined by the gestures using the Drop event. This ensures that only the logic defined by the view model is used to reorder items: You can reorder items in a CollectionView by reordering items in the underlying collection bound to the CollectionView.
Introduced in OS X 10.5, NSCollectionView offered a handy means of arranging a group of objects in a grid of identically-sized items displayed in a scrollable view. OS X 10.11 El Capitan gave NSCollectionView a major overhaul inspired by UICollectionView from iOS.
I wanted to get drag and drop working on an NSOutlineView, and I found Jonathan Dann's source code incredibly helpful.
You might want to check it out and try to modify it for use with NSCollectionView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With