NSOutlineView 拖放

发布于 2024-10-14 23:46:36 字数 3225 浏览 7 评论 0原文

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

2 -- 使用 CustomeCell,因为我需要自定义单元格

头文件: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];**

    }

并支持拖放实现了以下方法

        - (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 进行调试,但我无法查看上述函数的任何日志, 我错过了什么重要的方法吗? 支持拖放

In my application, NSOutlineView used as below,
1 -- Using CustomOutlineView because i want to control NSOutlineView Background,

2 -- Using CustomeCell, becuase i need to have customize Cell

Header File : 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;

and also i should be able to drag-n-drop within Outline view,
for having drag-n-drop i have done following,

  -(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];**

    }

and to Support drag-n-drop Implemented following method

        - (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");
    }

but still when i try to drag the row of NSOutlineView nothing is happening, even i tried to debug via NSLog but i couldn't see any log from above function,
Am i missing any important method ?
to support Drag-n-drop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2024-10-21 23:46:36

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

Based on your code, it looks like you're not returning anything for the ...writeItems... method. This method is supposed to return a BOOL (whether the items were successfully written or you're denying the drag). Returning nothing should be giving you a compiler warning ...

暮年 2024-10-21 23:46:36

你好,
终于摆脱了这个困扰
问题是

[firstColumn setWidth:25];

,所以拖动只能在左侧最多 25 像素处工作,我评论了这一点,然后是它的工作时间,但奇怪的是,我的大纲视图只有一列,用于绘图,它不检查限制,而是检查拖动- n-放弃检查,

谢谢 Joshua

HI,
FInally got able to rid of this,
The problem is

[firstColumn setWidth:25];

So drag was working only upto 25 pixel form the left, and i commented this, then its working time, but strange to see, my outline view has only one column , for drawing, its not checking the restriction but for drag-n-drop its checking,

Thanks Joshua

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文