NSTableView Drag and Drop

Discussion in 'Mac Programming' started by varsis, Jan 23, 2008.

  1. macrumors regular

    Joined:
    Nov 30, 2005
    #1
    Code:
    - (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:

    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
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    From the NSMutableArray documentation:

    So, retain your object before you remove it.
     
  3. thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #3
    Thanks so much. That helps alot.

    Chris
     

Share This Page