Drag and drop in NSTableView
Drag and drop in NSTableView
- Subject: Drag and drop in NSTableView
- From: Michèle Garoche <email@hidden>
- Date: Wed, 23 Jan 2002 23:16:26 +0100
Newbie question.
I'm working on the Programming with Cocoa book and get completely stuck
with implementing drag and drop in a tableView, though I browsed the
documentation and search the mailing lists archives.
I've noticed that the third function was never called. Obviously I'm
missing something.
Could someone help me? Thanks in advance
Here's the code I've implemented in the document file
#define MIGAarchivedPeoplePboardType @"MyCustomRaisMan2PboardType"
- (BOOL) tableView: (NSTableView *) tv
writeRows: (NSArray *) rows
toPasteboard: (NSPasteboard *) pboard
{
NSNumber *anIndex;
int aRow;
NSEnumerator *enumerator;
NSData *archivedPeople;
// Create the pasteboard
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard];
// Create a temporary autoreleased array for the records to be put
on the pasteboard
NSMutableArray *peopleToMove = [NSMutableArray array];
// Get the selected rows
enumerator = [tv selectedRowEnumerator];
// Traverse the enumeration
while ((anIndex = [enumerator nextObject]))
{
// Get the selected row
aRow = [anIndex intValue];
// Add the row to the array
[peopleToMove addObject: [employees objectAtIndex: aRow]];
}
// Archive the temp array into a data object
archivedPeople = [NSArchiver archivedDataWithRootObject:
peopleToMove];
// Prepare the pasteboard to receive data
[pb declareTypes: [NSArray arrayWithObject:
MIGAarchivedPeoplePboardType] owner: self];
// Write data to the pasteboard
[pb setData: archivedPeople forType: MIGAarchivedPeoplePboardType];
return YES;
}
- (NSDragOperation) tableView: (NSTableView *) tv
validateDrop: (id <NSDraggingInfo>) info
proposedRow: (int) row
proposedDropOperation: (NSTableViewDropOperation) op
{
// Return the pasteboard
NSPasteboard *pb = [info draggingPasteboard];
// Retrieve the types
NSString *type = [pb availableTypeFromArray:
[NSArray arrayWithObject: MIGAarchivedPeoplePboardType]];
// Check the type
if (type == MIGAarchivedPeoplePboardType)
{
return NSDragOperationCopy;
}
else
{
return NSDragOperationNone;
}
}
- (BOOL) tableView: (NSTableView *) tv
acceptDrop: (id <NSDraggingInfo>) info
row: (int) row
proposedDropOperation: (NSTableViewDropOperation) op
{
NSData *archivedPeople;
NSMutableArray *peopleCome;
// Return the pasteboard
NSPasteboard *pb = [info draggingPasteboard];
// Retrieve the types
NSString *type = [pb availableTypeFromArray:
[NSArray arrayWithObject: MIGAarchivedPeoplePboardType]];
// Check the type
if (type == MIGAarchivedPeoplePboardType)
{
// Read data from the pasteboard
archivedPeople = [pb dataForType: MIGAarchivedPeoplePboardType];
// Recreate the array of people
peopleCome = [NSUnarchiver unarchiveObjectWithData: archivedPeople];
// Add the people to the employees array
[employees addObjectsFromArray: peopleCome];
// Update the change count
[self updateChangeCount: NSChangeDone];
// Update the UI
[self updateUI];
return YES;
}
return NO;
}
Michhle