问题描述
我可能犯了一个简单的错误,但是我根本无法将文件拖放到NSCollectionView上,即使以最基本的方式工作也是如此.
There is probably a simple mistake that I'm making, but I simply cannot get dropping of files onto an NSCollectionView to work even in the most basic way.
在一个测试项目中,我在窗口上有一个NSCollectionView,而视图控制器既是其委托者,又是数据源.我希望能够将文件从Finder拖到该集合视图中.
In a test project, I have an NSCollectionView on a window, and the view controller is both its delegate and data source. I want to be able to drag files from the Finder onto this collection view.
通过阅读文档,我要做的就是:
From reading the docs, all I should have to do is:
注册拖动类型:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"Registering dragged types for collection view: %@", self.collectionView);
[self.collectionView registerForDraggedTypes:@[NSFilenamesPboardType]];
[self.collectionView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];
[self.collectionView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
}
然后实现这两种方法:
-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation {
NSLog(@"Validate drop: %@", draggingInfo);
return NSDragOperationMove;
}
-(BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation {
NSLog(@"Accept drop: %@", draggingInfo);
return YES;
}
但是,当我尝试将一个项目拖到集合视图上时,这两个方法都没有被调用过,这使我认为 registerForDraggedTypes:
调用未按预期工作.
But none of the two methods is ever called, when I try to drag an item onto the collection view, which makes me think that the registerForDraggedTypes:
call is not working as expected.
这里可能是什么问题?我还需要研究什么?
What can be the issue here? What else do I have to look into?
推荐答案
从OS X 10.11起, NSCollectionViewDelegate
方法采用索引路径而不是索引路径.例如
From OS X 10.11 the NSCollectionViewDelegate
methods take an index path instead of an index. For instance in
-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
proposedIndex:
参数替换为 proposedIndexPath:
- (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id <NSDraggingInfo>)draggingInfo proposedIndexPath:(NSIndexPath * __nonnull * __nonnull)proposedDropIndexPath dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
这篇关于无法拖放到NSCollectionView上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!