views:

463

answers:

2

Hi, I need to drag a row from NSTableView containing an image path and drop it over NSImageView, and the image of the dragged row should appear in the imageview. Help appreciated

+1  A: 

First, in your table data source, implement the necessary methods for table row dragging. You'll put data representing the row onto the drag pasteboard in one or more data types. One type you'll use for this is NSFilenamesPboardType, which takes an array of pathnames.

Then, make a subclass of NSImageView that can handle NSFilenamesPboardType in drops. (You'll need to implement methods from the NSDraggingDestination informal protocol.) Then make your image view an instance of this subclass, instead of NSImageView, and register that view for NSFilenamesPboardType.

Peter Hosey
+2  A: 

Thanks a lot. It really worked. I registered NSImageView and NSTableView for NSStringPboardType and NSFilenamesPboardType. Then in TableView delegate I used the following code.

- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
 NSString *string = [filePath objectAtIndex:[rowIndexes firstIndex]];
 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
 [pboard setString:string forType:NSStringPboardType];
 return YES;
}

And in NSImageView NSDragging Destination informal protocol, used following code.

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
 NSString *str = [[sender draggingPasteboard] stringForType:NSStringPboardType];
 myImage = [[NSImage alloc] initWithContentsOfFile:str];
 [self setImage:myImage];
 [self setNeedsDisplay: YES];
 return NSDragOperationCopy;
}

cheers :)

You only have to register the receiving view (the image view), not the sending view (the table view), for the dragged type. Unless you want to allow dropping on the table view as well—but that requires more data source methods.
Peter Hosey
Also, don't forget to release what you have allocked. See the Memory Management Programming Guide for Cocoa: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
Peter Hosey
Yes. you are right. I'll implement it.Thanks a lot once again. :)