objective-c - NSOutlineView拖放问题

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

亲爱的大家, 自过去 2-3 天以来,我一直在与 NSOutline View 进行斗争,在表格中执行拖放操作,但无法明白我做错了什么, 这就是我到目前为止所做的,

要求, 1 -- 我想在我的 View 中有一个透明的或带有一些背景图像的 NSoutlineview ,要做到这一点,我需要覆盖 NSOutlineView 的 drawRect 方法,这就是我所做的

在头文件中

@interface MyCustomOutlineView:NSOutlineView{
NSImage *pBackgroundImage;

}

- setBackgroundImage:(NSImage *)pImage;

在源文件中,我只是覆盖了drawRect和其他一些东西来绘制背景图像或使其透明,并且工作正常,

现在在我看来,我已经声明了 NSOutlineView 对象,

/* * 我的自定义 View 将能够绘制渐变和其他效果
*/

@interface OutlineParentView:MyCustomView<NSOutlineDataSource>{
   MyCustomOutlineView *pOutlineView;
}

@property(nonatomic,retain)MyCustomOutlineView *pOutlineView;

在源文件中,实现了以下方法, 该方法将从 initWIthFrame 中调用

#define OutlineViewPrivateDataType "MyOutlinePrivateData"
-(void)InitOutlineView{
    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];
    pOutlineView       = [[[CommUICustomOutlineView alloc] initWithFrame:clipViewBounds] autorelease];
    NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    ImageTextCell *pCell = [[ImageTextCell alloc]init];
    [firstColumn setDataCell:pCell];
    [pCell setDataDelegate:self];
    [firstColumn setWidth:25];
    [pOutlineView  addTableColumn:firstColumn];
    [pOutlineView setRowHeight:30];
    [pOutlineView setDataSource:self];

    /* setData delegate implemented in the MyOutlineView to handle some of event in this  
       interface if i don't write this, then i can't handle the menu event on the 
       Outlineview  
     */
    [pOutlineView setDataDelegate:self];

    **/* On this line i am getting one warning, saying OutlineParentView doesn't implement    
       NSOutlineView delegate protocol */**
    [pOutlineView setDelegate:self];
    [scrollView setDocumentView:pOutlineView];
    /* I don't want group row to be highlighted */
    [pOutlineView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
    [pOutlineView registerForDraggedTypes:
         [NSArray arrayWithObjects:OutlineViewPrivateDataType,nil]];

    [pOutlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];
    [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
        [self addSubview:scrollView];
    [self setAutoresizesSubviews:YES];
    [self log:@"Outline view created "];

}

与拖放相关的其他重要方法实现如下

- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteb oard:(NSPasteboard *)pboard{
    [self log:@"write Items"];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:items];
    [pboard declareTypes:[NSArray arrayWithObject:OutlineViewPrivateDataType] 
    owner:self];
    [pboard setData:data forType:OutlineViewPrivateDataType];
    return YES;
}

    - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
    {
        NSLog(@"validate Drop");
        return NSDragOperationEvery;
    }
    - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)index{
        [self log:@"inside accept drop for NSView "];
        return YES;
    }

我检查了苹果 DragnDrop 示例代码,但无法明白我做错了什么, 我相信,有些问题

[pOutlineView setDelegate:self]

功能,但我不知道如何解决这个问题, 我最近从 MyTableView 升级到 MyOutlineView,最近我能够在自定义表格 View 上执行拖放, 剩下的事情工作正常,比如 DataSource 等...

提前致谢 亲切的问候 罗汉

最佳答案

我正在设置 ColumnWidth,因此拖放操作适用于已设置的宽度,而不是整行,我的错误:(

 [firstColumn setWidth:25];

删除这条线工作正常

但是大家还面临着其他问题 Regarding NSoutlineView and WriteItem | Drag-and-Drop Problem

关于objective-c - NSOutlineView拖放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4889573/

相关文章:

iphone - 支持的界面方向和 shouldAutorotateToInterfaceOrientation

ios - 应用程序未运行时不会收到远程通知

objective-c - 在显示弹出窗口时保持 NSStatusBarButton 突出显示

objective-c - Cocoa - iTunes 轨道持久 ID - 脚本桥与分布式通知

qt - 在QTreeView中拖放,removeRows没有被调用

android - 通过拖放来订购RecyclerView的元素

iOS自定义UIActivityItemProvider分享图文

objective-c - 哪些方法会引发 NSInvalidArgumentException?

objective-c - 在 NSBundle 中使用 ConnectionKit 框架

javascript - React/AntDesign 如何使行可拖动? (表格拖动排序)