从NSOutlineView内拖放 [英] Drag and drop from within a NSOutlineView

查看:145
本文介绍了从NSOutlineView内拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想让用户能够重新排序并在NSOutlineView中嵌套和重新嵌套这些项目,但是我不需要从任何其他源(如文件或另一个视图)拖动项目。



我可以找到的所有例子都可以将项目拖到NSOutlineView中,而不仅仅是在本身内,似乎过于复杂。我假设这是可能的。



有人可以指出处理这种情况的文档吗?也许我只是错过了很明显的。

解决方案

我发现这个文件有些不清楚,在Lion中添加了新的方法。



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



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

  //注册方法从自己拖动的项目的视图。 
//在初始化期间调用
- (void)enableDragNDrop
{
[self.outlineView registerForDraggedTypes:[NSArray arrayWithObject:@DragID]];
}


- (id< NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item
{
//否如果<某些条件不符合>拖动

if(dontDrag){
return nil;
}

//除了所有的阻塞条件,
//返回一个粘贴板作者。

//在用于项目的类中实现一个uniqueStringRepresentation方法(或类似的)
//。
//如果您有其他方法可以获得您的项目的唯一字符串表示
//,您可以使用它。
NSString * stringRep = [item uniqueStringRepresentation];

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

[pboardItem setString:stringRep forType:@DragID];

return pboardItem; $($)

$ b - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id< NSDraggingInfo>)info SubmittedItem:(id)item proposedChildIndex:(NSInteger) index
{
BOOL dragOK = //找出如果拖动的项目可以在给定的索引

上的建议项目上删除
// if if (dragOK){
return NSDragOperationMove;
}
else {
return NSDragOperationNone;
}
}


- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id< NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
{
//将拖动的项目移动到数据模型
//中的新位置,并重新加载视图或移动视图中的行。
//这当然完全取决于你的实现
}

Apple的复杂表视图演示代码是非常有用的,如果有点复杂的解密。




I'm trying to figure out how to implement drag and drop from within a NSOutlineView to itself.

For example, I want the user to be able to reorder and nest and re-nest the items in the NSOutlineView, but I don't need to be able to drag items from any other source (like files or from another view).

All the examples I can find deal with dragging item into the NSOutlineView, not just within itself and seem overly complex. I assume this is possible.

Can someone point be to some docs that deal with just this case? Maybe I'm just missing the obvious.

解决方案

I have found the documentation of this to be somewhat unclear, and among other things the new ways to do it were added in Lion.

Assuming you need this for 10.7 or higher, then this could be a skeleton for you implementation:

In your data source/delegate class, implement:

// 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        
}

There is a good deal of code needed to be filled in for the last method, and regarding the animated movement, Apple's complex table view demo code mentioned in samir's comment is quite useful, if somewhat complex to decipher.

这篇关于从NSOutlineView内拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆