Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Drag item from NSTableView to item in NSOutlineView

Drag item from NSTableView to item in NSOutlineView
Thread Tools
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Aug 25, 2002, 11:39 PM
 
Can anyone tell me how I can drag an item from an NSTableView to an item in an NSOutlineView?

The purpose is to have an item from a flat list (the table view) added to an organised list (the outline view).

If someone can help me figure this out, I'd be grateful.
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Aug 26, 2002, 04:51 AM
 
Couldn't be easier with Cocoa:

Start by telling the NSOutlineView that it should receive drops. awakeFromNib in your window controller is normally a good place for this. Replace the ... below with whatever type you want to drag to the outline view. Either use the constants from NSPasteboard.h (NSStringPboardType, NSFilenamesPboardType, ...) or use your own type (like @"MyUniqueDragType") if you're going to drag a type of data that isn't covered by the standard types.

Code:
[myOutlineView registerForDraggedTypes:[NSArray arrayWithObjects:..., nil]];

Next add the following methods to your datasource for the NSTableView and NSOutlineView respectively (normally they are both the same as the window controller):

Code:
- (BOOL)tableView:(NSTableView *)tv writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard { // this is a trivial example that always puts the string foo // on the pasteboard when dragging [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self]; [pboard setString:@"foo" forType:NSStringPboardType]; return YES; } - (NSDragOperation)outlineView:(NSOutlineView*)olv validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index { // depending on info you might refuse the drag (return NSDragOperationNone) // or use some other operation return NSDragOperationGeneric; } - (BOOL)outlineView:(NSOutlineView*)olv acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index { // this is where you normally add the dragged object to the // data hierarchy for the outline view NSPasteboard *pb = [info draggingPasteboard]; NSString *s = [pb stringForType:NSStringPboardType]; NSLog(@"drop accepted: %@", s); return YES; }
Hope this helps

/Tobias
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Aug 26, 2002, 06:13 PM
 
Thanks muchly for the advice... I'll give it a go when I get a chance later in the week.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:12 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2