首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从NSOutlineView中拖放

从NSOutlineView中拖放
EN

Stack Overflow用户
提问于 2012-09-04 15:10:46
回答 2查看 5.8K关注 0票数 8

我正在尝试弄清楚如何实现从NSOutlineView内部到自身的拖放。

例如,我希望用户能够对NSOutlineView中的项进行重新排序、嵌套和重新嵌套,但我不需要能够从任何其他源(如文件或其他视图)拖动项。

我能找到的所有例子都涉及到将项目拖入NSOutlineView中,而不仅仅是在其内部,而且看起来过于复杂。我假设这是可能的。

有人能指出一些只处理这种情况的文档吗?也许我只是错过了显而易见的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-04 15:58:03

我发现这方面的文档有些不清楚,除了其他东西之外,Lion中还添加了新的方法。

假设您在10.7或更高版本中需要它,那么这可能是您实现的框架:

在您的数据源/委托类中,实现:

代码语言:javascript
复制
// A method to register the view for dragged items from itself. 
// Call it during initialization
-(void) enableDragNDrop
{
    [self.outlineView registerForDraggedTypes: [NSArray arrayWithObject: @"DragID"]];
}


- (id <NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item 
{
    // No dragging if <some condition isn't met>

    if ( dontDrag )  {
        return nil;
    }

    // With all the blocking conditions out of the way,
    // return a pasteboard writer.

    // Implement a uniqueStringRepresentation method (or similar)
    // in the classes that you use for your items.
    // If you have other ways to get a unique string representation
    // of your item, you can just use that.
    NSString *stringRep = [item uniqueStringRepresentation];

    NSPasteboardItem *pboardItem = [[NSPasteboardItem alloc] init];

    [pboardItem setString:stringRep forType: @"DragID"];

    return pboardItem;
}


- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
{
    BOOL dragOK = // Figure out if your dragged item(s) can be dropped
                  // on the proposed item at the index given

    if ( dragOK ) {
        return NSDragOperationMove;
    }
    else {
        return NSDragOperationNone;
    }
}


- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)index
{           
    // Move the dragged item(s) to their new position in the data model
    // and reload the view or move the rows in the view.
    // This is of course quite dependent on your implementation        
}

最后一种方法需要填写大量代码,关于动画运动,samir的评论中提到的苹果的complex table view demo code非常有用,尽管有点复杂。

票数 14
EN

Stack Overflow用户

发布于 2013-10-22 22:17:16

我已经创建了一个具有NSOutlineViewsmall sample project,您可以在其中添加和删除项目。

今天,我已经实现了基于@Monolo的答案(See changes)的拖放支持。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12258426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档