Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSOutLineView drag n'drop

I use this method for enable drag n'drop on my NSOutLineView, with this code I disable the auto expand of children level, all work but I want to add a feature, I want to allow drag n'drop only on the current level, with this code I disable auto expand but not the children destination, so I can copy my item into the children level.
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard{
    
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[items objectAtIndex:0]];
        
        if ([[items objectAtIndex:0] isKindOfClass:[Testo class]]) {
            
            [pboard declareTypes:[NSArray arrayWithObject:@Testo] owner:self];
            [pboard setData:data forType:@Testo];
            return YES;
        }
        else if ([[items objectAtIndex:0] isKindOfClass:[Sezione class]]) {
    
            [pboard declareTypes:[NSArray arrayWithObject:@Sezione] owner:self];
            [pboard setData:data forType:@Sezione];
            return YES;
        }
        else {
    
            return NO;
        }
    }

 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index{
        // Add code here to validate the drop
        isEditing = YES;
        
        if ([item isKindOfClass:[Materia class]] ) {
            return NSDragOperationEvery;
        }
        else if ([item isKindOfClass:[Sezione class]]) {
            return NSDragOperationEvery;
        }
        else {
            return NSDragOperationNone;
        }
    }
    
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index{
    
        NSData *rowData = nil;
        NSMutableArray *arr = nil;
        NSPasteboard *pboard = [info draggingPasteboard];
        
        if ([item isKindOfClass:[Materia class]]) {
            rowData = [pboard dataForType:@Sezione];
            arr = [item sezioni];
        }
        else if ([item isKindOfClass:[Sezione class]]) {
            rowData = [pboard dataForType:@Testo];
            arr = [item testi];
        }
    
        NSInteger dragRow = [arr indexOfObject:[NSKeyedUnarchiver unarchiveObjectWithData:rowData]];

        if (dragRow < index) {
            [arr insertObject:[arr objectAtIndex:dragRow] atIndex:index];
            [arr removeObjectAtIndex:dragRow];
        }
        else {
            id oldItem = [arr objectAtIndex:dragRow];   
            [arr removeObjectAtIndex:dragRow];
            [arr insertObject:oldItem atIndex:index];
        }
        isEditing = NO;
        
        [self.myOutlineView noteNumberOfRowsChanged];
        [self.myOutlineView reloadData];        
        return YES;
    }
- (BOOL) outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item {
        if (isEditing == YES) {
            return NO;
        }
        else {
            return YES;
        }
        
    }
Tagged:
Sign In or Register to comment.