0

The last three days I've been trying to create a single row with 4 icons, which you can re-arrange by dragging. I've tried a lot of different approaches, NSCollectionView so far has been the best one. I can now create a NSCollectionView programmatically and add Items to it with an NSArray. However, I still cannot re-arrange the icons/NSCollectionViewItems. I'm pretty stuck right now and hope one of you guys can point me in the right direction.

I've read a lot of similar questions on stackoverflow (and the entire internet). There it became clear that I needed to add methods from NSCollectionViewDelegate:

- (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet     *)indexes toPasteboard:(NSPasteboard *)pasteboard

-(BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent*)event

-(BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id < NSDraggingInfo >)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation

-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id < NSDraggingInfo >)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation

-(NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes

I've added these (with return values) to my NSCollectionView subclass, and still I get nothing. So the question I have right now is: should I create a subclass of the NSCollectionViewDelegate and override these methods there?

But is this really going in the right direction? (As you've probably noticed, I'm a Cocoa beginner, so I'm pretty lost right now as I find the NSCollectionView class very confusing.)

Thanks in advance, Frans

Frans
  • 57
  • 7

1 Answers1

0

This doesn't go in a subclass. You are to implement this in a delegate (usually the controller supplying content to the collection view). Set a controller as the collection view's delegate (the object it asks for decision making) and implement those methods in that controller.

Some very important reading.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Thanks, I've now implemented the first 4 methods above in my delegate. The first 3 all return YES, the 4th one returns NSDragOperationMove. canDragItemsAtIndexes is the only method being called. Is there any tutorial I can follow where I could learn what to fill these methods with? (Except the return statement) – Frans Jan 24 '13 at 21:24
  • I've found http://stackoverflow.com/questions/5825833/nscollectionview-drag-and-dropping-most-delegate-events-not-getting-called and added registerForDraggedTypes and I've got it almost working. I can move the items around, they don't stick to their new location yet. I assume that's not too hard. When all is finished, I'll post the project on the web – Frans Jan 24 '13 at 22:05
  • Post your code - it sounds like you're not rearranging the content your controller is providing in response to the proposed drop (maybe you're not validating it; maybe you're not "accepting" and reordering). It won't do this for you because it's up to you (rather, your controller) to decide what it means (because maybe you don't want to allow this or that). – Joshua Nozzi Jan 25 '13 at 04:50