我正在尝试创建一个集合视图,该视图将接受来自Finder的文件。我的委托人的validateDrop
方法始终返回NSDragOperationGeneric
。如果建议的放置操作为NSCollectionViewDropBefore
,则一切正常。但是,如果在我释放鼠标按钮时建议的放置操作为NSCollectionViewDropOn
,则永远不会调用acceptDrop
委托方法。我可能会缺少什么?
收藏夹视图项仅包含图像。我按原样使用NSCollectionViewItem
,而不是子类。
初始化:
[self.collectionView registerForDraggedTypes: @[NSPasteboardTypeFileURL]];
NSNib* itemNib = [[NSNib alloc] initWithNibNamed: @"IconItemView" bundle: nil];
[self.collectionView registerNib: itemNib forItemWithIdentifier: @"app"];
我的委托方法:
- (NSDragOperation)collectionView:(NSCollectionView *)collectionView
validateDrop:(id<NSDraggingInfo>)draggingInfo
proposedIndexPath:(NSIndexPath * _Nonnull *)proposedDropIndexPath
dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
{
NSLog(@"Validate drop op %d", (int)*proposedDropOperation );
return NSDragOperationGeneric;
}
- (BOOL)collectionView:(NSCollectionView *)collectionView
acceptDrop:(id<NSDraggingInfo>)draggingInfo
indexPath:(NSIndexPath *)indexPath
dropOperation:(NSCollectionViewDropOperation)dropOperation
{
NSLog(@"Accept drop op %d", (int)dropOperation );
[draggingInfo enumerateDraggingItemsWithOptions: 0
forView: self.collectionView
classes: @[ [NSURL class] ]
searchOptions: @{}
usingBlock:
^(NSDraggingItem * _Nonnull draggingItem, NSInteger idx, BOOL * _Nonnull stop)
{
NSURL* itemURL = draggingItem.item;
NSImage* theImage = [[NSWorkspace sharedWorkspace]
iconForFile: itemURL.path];
NSDictionary* itemRec = @{
@"icon": theImage,
@"url": itemURL
};
NSMutableArray* secArray = [self arrayForSection: indexPath.section];
[secArray insertObject: itemRec atIndex: indexPath.item];
}];
[self.collectionView reloadData];
return YES;
}