NSOutlineView Drag-n-Drop

时间:2011-01-31 11:00:33

标签: objective-c cocoa drag-and-drop nsoutlineview

在我的应用程序中,NSOutlineView使用如下, 1 - 使用CustomOutlineView,因为我想控制NSOutlineView背景,

2 - 使用CustomeCell,因为我需要自定义Cell

头文件:MyListView.h

/* 
 MyUICustomView is the Subclass from NSView and this is i need to have 
 for some other my application purpose 
*/
@interface MyListView : MyUICustomView<NSOutlineViewDataSource> 
{
      // MyCustomOutlineview because, i need to override DrawRect Method to have   
      //   customized background 
      MyCustomOutlineView *pMyOutlineView;

}

@property(nonatomic,retain)MyCustomOutlineView *pMyOutlineView;

我也应该能够在大纲视图中拖放 因为我已经完成了拖拉操作,

  -(void)InitOutlineView{
        // Creating outline view 
        NSRect          scrollFrame = [self bounds];
    NSScrollView*   scrollView  = [[[NSScrollView alloc] initWithFrame:scrollFrame] autorelease];

    [scrollView setBorderType:NSNoBorder];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setHasHorizontalScroller:NO];
    [scrollView setAutohidesScrollers:YES];
    [scrollView setDrawsBackground: NO];

    NSRect          clipViewBounds  = [[scrollView contentView] bounds];
    pMyOutlineView       = [[[MyCustomOutlineView alloc] initWithFrame:clipViewBounds] autorelease];


    NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
#ifdef ENABLE_CUSTOM_CELL
        // Becuase cell should have Image, Header Info and brief detail in small font, 
        // so i need to have custom cell 
    ImageTextCell *pCell = [[ImageTextCell alloc]init];
    [firstColumn setDataCell:pCell];
        // SO i can fill the data 
    [pCell setDataDelegate:self];
# endif

        [pMyOutlineView setDataSource:self];
        /* This is to tell MyCustomOutlineView to handle the context menu */
    [pMyOutlineView setDataDelegate:self];
        [scrollView setDocumentView:pCTOutlineView];

        [pMyOutlineView  addTableColumn:firstColumn];
        [pMyOutlineView registerForDraggedTypes:
         [NSArray arrayWithObjects:OutlinePrivateTableViewDataType,nil]];

        [pMyOutlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];**

    }

并支持drag-n-drop实现以下方法

        - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard{
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:items];
        [pboard declareTypes:[NSArray arrayWithObject:OutlinePrivateTableViewDataType] owner:self];
        [pboard setData:data forType:OutlinePrivateTableViewDataType];
        return YES;

    }


    - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index{
    // Add code here to validate the drop

    NSLog(@"validate Drop");
    return NSDragOperationEvery;
}

        - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)index{
          NSLog(@"validate Drop");
    }

但是当我尝试拖动NSOutlineView的行时,没有发生任何事情,即使我试图通过NSLog进行调试,但我无法从上面的功能看到任何日志, 我错过了任何重要的方法吗? 支持Drag-n-drop

2 个答案:

答案 0 :(得分:1)

根据您的代码,看起来您没有为... writeItems ...方法返回任何内容。这个方法应该返回一个BOOL(无论是成功写入项目还是拒绝拖动)。什么都不返,应该给你一个编译器警告......

答案 1 :(得分:0)

HI, FInally能够摆脱这个, 问题是

[firstColumn setWidth:25];

所以拖动左边只有25像素的工作,我评论了这个,然后是它的工作时间,但很奇怪,我的大纲视图只有一列,用于绘图,它没有检查限制,而是拖动 - n-drop检查,

感谢约书亚