gpt4 book ai didi

cocoa - 从 NSOutlineView 中拖放

转载 作者:行者123 更新时间:2023-12-03 16:05:46 26 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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

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

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

最后一个方法需要填写大量代码,关于动画运动,Apple 的 complex table view demo code萨米尔的评论中提到的非常有用,尽管解读起来有些复杂。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com