Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop within NSCollectionView

Tags:

cocoa

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.

like image 377
Nektarios Avatar asked Aug 27 '10 15:08

Nektarios


People also ask

How do I use nscollectionviewdelegateflowlayout?

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.

Why does my nscollectionviewitem return nil?

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.

How to reorder items in a collectionview using gestures?

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.

What is nscollectionview in Mac OS X?

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.


1 Answers

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.

like image 82
John Gallagher Avatar answered Oct 05 '22 06:10

John Gallagher