Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
Objective c 从NSTableView拖放到NSImageView_Objective C_Cocoa_Drag And Drop - Fatal编程技术网

Objective c 从NSTableView拖放到NSImageView

Objective c 从NSTableView拖放到NSImageView,objective-c,cocoa,drag-and-drop,Objective C,Cocoa,Drag And Drop,我需要从NSTableView中拖动一行,其中包含一个图像路径,然后将其放到NSImageView上,被拖动行的图像应该显示在imageview中。帮助首先,在您的中,实施必要的表格行拖动方法。您将以一种或多种数据类型将代表行的数据放在拖动粘贴板上。您将使用的一种类型是NSFilenamesPboardType,它接受一个路径名数组 然后,创建NSImageView的子类,该子类可以直接处理NSFilenamesPboardType。(您需要从中实现方法。)然后将图像视图设置为该子类的实例,而不

我需要从NSTableView中拖动一行,其中包含一个图像路径,然后将其放到NSImageView上,被拖动行的图像应该显示在imageview中。帮助

首先,在您的中,实施必要的表格行拖动方法。您将以一种或多种数据类型将代表行的数据放在拖动粘贴板上。您将使用的一种类型是
NSFilenamesPboardType
,它接受一个路径名数组


然后,创建NSImageView的子类,该子类可以直接处理
NSFilenamesPboardType
。(您需要从中实现方法。)然后将图像视图设置为该子类的实例,而不是NSImageView,并将该视图设置为
NSFilenamesPboardType

非常感谢。这真的奏效了。我为NSStringPboardType和NSFileNamespoardType注册了NSImageView和NSTableView。然后在TableView中,我使用了 下面的代码

- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
    NSString *string = [filePath objectAtIndex:[rowIndexes firstIndex]];
    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
    [pboard setString:string forType:NSStringPboardType];
    return YES;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSString *str = [[sender draggingPasteboard] stringForType:NSStringPboardType];
    myImage = [[NSImage alloc] initWithContentsOfFile:str];
    [self setImage:myImage];
    [self setNeedsDisplay: YES];
    return NSDragOperationCopy;
}
在NSImageView协议中,使用以下代码

- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
    NSString *string = [filePath objectAtIndex:[rowIndexes firstIndex]];
    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
    [pboard setString:string forType:NSStringPboardType];
    return YES;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSString *str = [[sender draggingPasteboard] stringForType:NSStringPboardType];
    myImage = [[NSImage alloc] initWithContentsOfFile:str];
    [self setImage:myImage];
    [self setNeedsDisplay: YES];
    return NSDragOperationCopy;
}
-(nsdragooperation)draggingented:(id)发送方
{
NSString*str=[[sender draggingPasteboard]stringForType:NSStringPboardType];
myImage=[[NSImage alloc]initWithContentsOfFile:str];
[自我设置图像:myImage];
[自我设置需要显示:是];
返回NSDRAGO操作副本;
}

干杯:)

对于拖动的类型,您只需注册接收视图(图像视图),而不必注册发送视图(表格视图)。除非您也希望允许在表视图上进行删除,但这需要更多的数据源方法。另外,不要忘记释放您已分配的内容。有关Cocoa的详细信息,请参阅《内存管理编程指南》:是。你是对的。我会实施的。再次非常感谢。:)