Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NSCollectionView拖放示例_Macos_Cocoa - Fatal编程技术网

Macos NSCollectionView拖放示例

Macos NSCollectionView拖放示例,macos,cocoa,Macos,Cocoa,我正在尝试在NSCollectionView中实现拖放,这将允许在视图中重新排列单元格。我设置了委托并实现了以下方法: -(BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard { NSLog(@"Write Items at indexes : %@", indexes

我正在尝试在
NSCollectionView
中实现拖放,这将允许在视图中重新排列单元格。我设置了委托并实现了以下方法:

-(BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard {
    NSLog(@"Write Items at indexes : %@", indexes);
    return YES;
}

- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event {
    NSLog(@"Can Drag");
    return YES;
}

- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation {
    NSLog(@"Accept Drop");
    return YES;
}

-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation {
    NSLog(@"Validate Drop");
    return NSDragOperationMove;
}
-(BOOL)collectionView:(NSCollectionView*)collectionView writeItemSatiIndexes:(NSIndexSet*)粘贴板索引:(NSPasteboard*)粘贴板{
NSLog(@“在索引处写入项:%@”,索引);
返回YES;
}
-(BOOL)collectionView:(NSCollectionView*)collectionView CandRagItemSatiIndex:(NSIndexSet*)带有事件的索引:(NSEvent*)事件{
NSLog(@“可以拖动”);
返回YES;
}
-(BOOL)collectionView:(NSCollectionView*)collectionView acceptDrop:(id)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionView dropOperation)dropOperation{
NSLog(“接受丢弃”);
返回YES;
}
-(NSDragOperation)collectionView:(NSCollectionView*)collectionView validateDrop:(id)DragginInfo proposedIndex:(NSInteger*)proposedDropOperation:(NSCollectionViewDropOperation*)proposedDropOperation{
NSLog(@“验证删除”);
返回NSDragOperationMove;
}

我不知道该怎么进一步。有了这一点,我可以看到现在我可以在单个集合项周围拖动,但是如何才能使
拖放

您只实现了委托方法,但某些方法中没有逻辑。例如,要在集合项周围拖动,我将添加以下逻辑:

-(BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard {
    NSData *indexData = [NSKeyedArchiver archivedDataWithRootObject:indexes];
    [pasteboard setDraggedTypes:@["my_drag_type_id"]];
    [pasteboard setData:indexData forType"@"my_drag_type_id"];
    // Here we temporarily store the index of the Cell, 
    // being dragged to pasteboard.
    return YES;
}

- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation {
    NSPasteboard *pBoard = [draggingInfo draggingPasteboard];
    NSData *indexData = [pBoard dataForType:@"my_drag_type_id"];
    NSIndexSet *indexes = [NSKeyedUnarchiver unarchiveObjectWithData:indexData];
    NSInteger draggedCell = [indexes firstIndex];
    // Now we know the Original Index (draggedCell) and the 
    // index of destination (index). Simply swap them in the collection view array.
    return YES;
}

并确保已将集合视图设置为可选。

除了上面提到的GoodSp33d之外,您还缺少接受拖放所需的
验证
委托功能。在Swift中,这是:

func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation
func collectionView(collectionView:NSCollectionView,validateDrop DragginInfo:nsDragginInfo,proposedIndexPath proposedDropIndexPath:AutoreleasingUnsafemtablePointer,dropOperation proposedDropOperation:UnsafemtablePointer)->NSDragOperation
注意返回值nsdragooperation。此方法应包含准确确定尝试何种拖动操作并返回此值的代码。返回错误的东西可能会导致一些非常恼人的错误


进一步注意,为了支持此类操作,您正在使用的集合视图布局类还必须支持拖放。Flow layout应该开箱即用,但如果您使用的是自定义布局,则可能需要对其进行调整以支持拖放,以便集合视图可以检测有效的拖放目标并为其确定合适的索引路径。

hello user88975,您是否解决了此问题?我面临着完全相同的问题,我找不到任何帮助。GoodSp33d,什么是
KL_DRAG_类型
TYPE它在Swift 3中无效?@JFS找到解决方案了吗?@JFS collectionView.register(forDraggedTypes:[NSPasteboardTypeString])
func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation