NSTableView drag & drop issues

Discussion in 'Mac Programming' started by pcwiz, Aug 10, 2009.

  1. pcwiz macrumors member

    Joined:
    May 28, 2008
    #1
    Hi,

    I'm having an issue with implementing drag and drop in my table view. I've followed various tutorials out there but none of them seem to work.

    First of all, I have my my table view set up with the delegate and outlet and all. Then in the class I define my drag type:

    Code:
    #define AppDrag @"app"
    And then I register the drag type in awakeFromNib:

    Code:
    [table registerForDraggedTypes:[NSArray arrayWithObject:AppDrag]];
    And then there are the 3 methods for drag and drop:

    Code:
    - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
    {
    	NSLog(@"write rows with indexes");
        return YES;
    }
    - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
    {
    	NSLog(@"validate drop");
    	NSPasteboard *pb = [info draggingPasteboard];
    	NSString * type = [pb availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]];
    	
    	if(type != nil && [type hasSuffix:@"app"]){
    		return NSDragOperationGeneric;
    	}
        return NSDragOperationNone;
    }
    
    - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
    			  row:(int)row dropOperation:(NSTableViewDropOperation)operation
    {
    	NSLog(@"accept drop");
    	//Get the files from the drop
    	NSArray * files = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
    	
    	[self addAppsFromArray:files];
    	
    	if([applications count] > 0) return YES;
    	return NO;
    	
    }
    
    Basically the app will let you drag apps from the Finder into the table view, and the "addAppsFromArray" method will deal with adding the app info to the table view.

    However this does not seem to work. When I drag an app into the table view the green + does not appear. I have put an NSLog in the beginning of each of the methods to see if they are being called, and I found out that none of them are being called.

    I am confused as to what I should do next

    Thanks
     
  2. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #2
    Try to go the other way. Drag from your app to wherever. In the second to last function, return NSDragOperationCopy. Just to see if it is working. Add a variable in your header file to store the dragged rowIndexes: " id dragData;"
    Code:
    - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
    {
        dragData = rowIndexes; //global variable to hang on to what is dragged
         
        return YES;
    }
    - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
    {
      return NSDragOperationCopy; // copy will you give a visual clue if something is happing.  Maybe it's just me, but NSDragOperationGeneric is always giving me problems. 
    }
    
    - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
    			  row:(int)row dropOperation:(NSTableViewDropOperation)operation
    {
    	NSLog(@"nbr of dragged items: %i", [dragData count]);
           
    return YES;
    }
    
    If this doesn't work, then something is wrong with you outlets.
     
  3. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #3
    I just started to go through the Drag & Drop exercises so if you wait a day or two I may be able to help you. I am able now to drag from my Application into another application. In my case I try to drag and drop an image.

    I did implemented only three methosd but I do not se them in your application, mouseDown, mouseDragged, dragImage.

    Good Luck
    /petron
     
  4. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #4
    Those methods are not required for a NSTableView, NSBrowserView or NSOutlineView. These three classes have their own methods for drag and drop.

    The methods you are mentioned are for any NSView subclass, which also includes the three listed above. You can overwrite them, if you want to finetune the drag and drop behaviour e.g. a custom drag icon.
     
  5. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #5
    Thank you MrFusion for the explanation.

    I do most of the job in the "dragImage" and I use as well "draggingSourceOperationMaskForLocal:" that I forgot to mention earlier.

    mouseDown picks up just the location coordinates I need to create an image of the dragged area.

    Thanks again.
    /petron
     

Share This Page