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
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
.
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 :)