NSTableView drag from Finder Problem
NSTableView drag from Finder Problem
- Subject: NSTableView drag from Finder Problem
- From: Support <email@hidden>
- Date: Wed, 26 Aug 2009 17:10:31 +0100
I fear that I may be asking a daft question here - but I've been puzzling over this for a while now and it's confusing the hell out of me. Probably only a quick fix too! I'm trying to implement NSTableView so that files can be dragged in from Finder. The NSTableView is within an NSSplitView (just in case that makes a difference) and it uses an NSMutableArray as the datasource.
The NSTableView uses the same class as datasource and delegate, for sent actions and for the referencing outlet. They all appear to be hooked up correctly.
The code for that class is as follows:
@implementation LibraryView
//------- Initialize ----------------//
- (id)init
{
self = [super init];
if (self)
{
[unseenLibraryView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
NSLog(@"Debug - Initializing");
}
return self;
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
return [unseenLibrary count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)row
{
id theRecord, theValue;
theRecord = [unseenLibrary objectAtIndex:row];
theValue = [theRecord objectForKey:[aTableColumn identifier]];
if ([theValue length] == 0) theValue = @"";
return theValue;
}
#pragma mark -
#pragma mark Drag and Drop Handler
//------- Drag and Drop Handler-------//
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes
:(NSIndexSet *)rowIndexes toPasteboard
:(NSPasteboard*)pboard
{
unsigned indexBuffer[[rowIndexes count]];
unsigned limit = [rowIndexes getIndexes:indexBuffer maxCount:[rowIndexes count]
inIndexRange:NULL];
unsigned idx;
NSMutableArray *fileNames = [[NSMutableArray alloc] init];
for (idx = 0; idx < limit; idx++)
{
id record = [unseenLibrary objectAtIndex:indexBuffer[idx]];
[fileNames addObject:[record objectForKey:@"filepath"]];
}
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
[pboard setPropertyList:fileNames forType:NSFilenamesPboardType];
[fileNames release];
return YES;
}
- (NSDragOperation)tableView:(NSTableView*)table validateDrop
:(id <NSDraggingInfo>)info proposedRow
:(int)row proposedDropOperation
:(NSTableViewDropOperation)operation
{
NSLog(@"debug validate drop");
// Make drops at the end of the table go to the end.
if (row == -1)
{
row = [table numberOfRows];
operation = NSTableViewDropAbove;
[table setDropRow:row dropOperation:operation];
}
// We don't ever want to drop onto a row, only between rows.
if (operation == NSTableViewDropOn)
[table setDropRow:(row+1) dropOperation:NSTableViewDropAbove];
return NSTableViewDropAbove;
}
- (BOOL)tableView:(NSTableView*)table acceptDrop:(id <NSDraggingInfo>)info row
:(int)dropRow dropOperation
:(NSTableViewDropOperation)op;
{
NSLog(@"debug accept drop");
NSPasteboard *pb = [info draggingPasteboard];
BOOL accepted = NO;
NSArray *array;
if (!accepted && (array = [pb propertyListForType:NSFilenamesPboardType]) != NULL)
{
[self addFiles:[array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] atIndex:dropRow];
accepted = YES;
}
return accepted;
}
#pragma mark -
#pragma mark Table Update Handler
- (void)updateLibraryTable
{
// Tell the table to reload.
[unseenLibraryView reloadData];
}
//------- Add files to table ------//
- (void)addFiles:(NSArray*)filenames atIndex:(unsigned)index
{
NSLog(@"debug add files");
unsigned int i = 0, count = [filenames count];
if (count == 0)
return;
for (i = 0; i < count; ++i)
{
NSString *path = [filenames objectAtIndex:i];
NSLog(@"debug %@",path);
[self updateLibraryTable];
}
}
@end
I can see from the debug that the init function is called and that the dragged types are therefore registered. Alas, ValidateDrop and AcceptDrop are never called. Does anyone have any idea what I've done wrong? All suggestions would be gratefully received.
Regards,
Pascal
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden