Wednesday, April 11, 2012

Implementing drag and drop in NSTableView

Can anyone help me to implement drag and drop in an NSTableView? I used this code below, but these methods are not getting called during execution.



- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:@".gif"] owner:self];
[pboard setData:data forType:@".gif"];
return YES;
}

- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op
{
// Add code here to validate the drop

if (row > [ m_imageArray count])
return NSDragOperationNone;

if (nil == [info draggingSource]) // From other application
{
return NSDragOperationNone;
}
else if (self == [info draggingSource]) // From self
{
return NSDragOperationNone;
}
else // From other documents
{
[tv setDropRow: row dropOperation: NSTableViewDropAbove];
return NSDragOperationCopy;
}

NSLog(@"validate Drop");
return NSDragOperationCopy;
}


- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:@".gif"];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSInteger dragRow = [rowIndexes firstIndex];

// Move the specified row to its new location...
}




No comments:

Post a Comment