1
votes

I would like to figure out how to perform drag and drop from strings in a table view onto a collection view. I know there are delegate methods for collectionView drag and drop but can't find any examples of how to implement them. I have my collection view set up, it seems to be working correctly but don't know how to finish.

Any help is appreciated.

Update: The collection view setup I am working with has 3 NSTextFields and 2 check boxes for each collection item. There is also a tableView in the same view. The table view is passed an a MutableArray of strings. I want to be able to drag string values from the table view rows into the appropriate textFields in the collection view item.

This is different from the typical way drag and drop is used for collection views.

1

1 Answers

0
votes

I am going to answer my own question because I spent quite a bit of time today trying to figure this out, Many people struggle with this procedure and mostly because I feel bad I keep asking the Stack community all these collection view questions all week:

I discovered the default behavior of the NSTextField actually allows a drop if it is in focus. The problem is that I need the appropriate NSTextField to auto focus on mouse enetered event. So as things turned out, I did not even need the NSCollectionView drag and drop delegates. I needed the NSTableView drag delegates and I needed to subclass the NSTextField and implement mouse event (drop) delegates in it.

so my class for the original collectionViewItem look like this:

//someClass.h 

@interface SomeClass : NSObject{

    IBOutlet NSTextField *field1_;
    IBOutlet NSTextField *field2_;
    IBOutlet NSTextField *field3_;

    IBOutlet NSButton *chkBox1_; 
    IBOutlet NSButton *chkBox2_;       

}     
  @porperty(readwrite, copy) NSTextField *filed1_;

properties are made for all 5 outlets for binding purposes. If you follow the tutorial on the Mac OSX Dev Library ; Collection View Programming Guide, it walks you through the process of setting up a collection View but it uses bindings.

So now the key is to set up a textField sub class //MyNSTextField.h #import @interface MyNSTextField : NSTextField{

    //mouse positioning 
    unsigned long last_;
}

//MyNSTextField.m
#import "MtTextField.h"

@implementation 

-(void)dealloc{
    [super dealloc];
}

-(void)awakeFromNib{

//register for dragged types
//any other nib stuff
}

//three required mouse events
-(unsigned long)draggingEntered:(id<NSDraggingInfo>)sender{

    //this forces the textfield to focus before drop
    [self.window makeFirstResponder: self];

   NSPasebord *pBoard;
   self->last_ = DragOperationNone;
   pBoard = [sender draggingPastboard];

  return self->last_;

}

-(unsigned long)draggingUpdated:(id<NSDraggingInfo>)sender{

    return self->last_;

}


-(void)draggingExited:sender{

   if([sender draggingSource] != self){

         self->last = NSDragOperationNone;

    }


  }   

}// end class

now just go back to the original class and change the name of your textField outlets from NSTextField to MyNSTextField and in the collection view, select each textfield and assign it the new class name in the inspector and as long as you had your tableview drag delegates set up, or if your are dragging from some other source, make sure you have the appropriate dragging source delegates set and it should work.