PDA

View Full Version : NSTableView Drag and Drop




varsis
Jan 23, 2008, 08:18 PM
- (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:MyPrivateTableViewDataType] owner:self];
[pboard setData:data forType:MyPrivateTableViewDataType];
return YES;}

- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
{
// Add code here to validate the drop
NSLog(@"validate Drop");
return NSDragOperationEvery;
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
row:(int)row dropOperation:(NSTableViewDropOperation)operation
{

NSPasteboard* pboard = [info draggingPasteboard];

//NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
NSData* rowData = [pboard dataForType:MyPrivateTableViewDataType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
int dragRow = [rowIndexes firstIndex];


//if (files != nil) {
//NSLog (@"Before Insert");
//[self addDragImage:files];
//} else {
NSLog (@"%i drag row",dragRow);
NSLog (@"%i Drop into Row",row);
int countIndex = [rowIndexes count];

id current = [array objectAtIndex:[rowIndexes firstIndex]];
[array removeObjectAtIndex:[rowIndexes firstIndex]];
[array insertObject:current atIndex:row];
//}
NSLog (@"%i Drop into Row",[array count]);
[table reloadData];
NSLog (@"Reload Table and Set Object");
return YES;

}


I have the above code to allow sorting within my NSTableView the problem is that when I add new objects into the array is causes problems. The application freezes up and crashes. The nstable display uses the following code:

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)row {
id theRecord, theValue; //define used variable
theValue = [[NSObject init] alloc];

if(row >= 0 && row < [array count]) {

theRecord = [array objectAtIndex:row];// row of Dictionary
theValue = [theRecord objectForKey:[aTableColumn identifier]]; // Get Object With KEY of Column

}

return theValue;



}

Any help would be good. The thing it screws up on is getting the objectForKey: in the above code. (Like I can't access the record.)


chris



kainjow
Jan 23, 2008, 09:51 PM
From the NSMutableArray documentation:

Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection, when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated. If your program keeps a reference to such an object, the reference will become invalid unless you send the object a retain message before itís removed from the array. For example, if anObject is not retained before it is removed from the array, the third statement below could result in a runtime error:

id anObject = [[anArray objectAtIndex:0] retain];
[anArray removeObjectAtIndex:0];
[anObject someMessage];

So, retain your object before you remove it.

varsis
Jan 24, 2008, 03:38 AM
From the NSMutableArray documentation:



So, retain your object before you remove it.
Thanks so much. That helps alot.

Chris