How to support Drag & Drop for OutlineView

I have taken 1 days to add the drag & drop feature for NSOutlineView,actually,before it took me 3 days to understand how NSOutlineView work.

Okay, let's start!

First,you should register the data type you want to drag and drop and make the NSOutlineView support drag and drop.

Please do it like this:

[outlineView registerForDraggedTypes:[NSArray arrayWithObjects: @"CustomType.2",nil]];
[outlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];
The type should be a array,and in general,we use custom type,so you should use a string to define it.and there is a point need to notice,it is that at best you should make the string's format is @"abc.abc",if you don't do it like this,maybe you will be failed!

Then you need to override some methods to response the user's drag and drop operations in the NSOutlineView,They are:

- (BOOL) outlineView: (NSOutlineView *) outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:items];
    [pasteboard declareTypes:[NSArray arrayWithObject: @"CustomType.2"] owner:self];
    [pasteboard setData:data forType: @"CustomType.2"];
    return YES;
}

- (NSDragOperation) outlineView: (NSOutlineView *) outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
{
    NSLog(@"Drop 1");
    return NSDragOperationEvery;
}

- (BOOL) outlineView: (NSOutlineView *) outlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
{
    NSLog(@"Drop 2");
    //do something...
    //if return NO,the drag & drop operation will not be completed.
    return YES;
}

The other point need to notice is that if table view also need to support,the first method maybe some difference,you should reference the documentation.

and the data type used in the first method should be same to the type registered.

If anyone need the demo,please leave your email,and I will send to you~

转载于:https://my.oschina.net/u/913344/blog/109355

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值