在NSTableView中实现拖放

在NSTableView中实现拖放

本文介绍了在NSTableView中实现拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我实现拖放在NSTableView?我使用下面的代码,但是这些方法在执行过程中没有被调用。

   - (BOOL)tableView:(NSTableView * tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard 
{
//将行号复制到粘贴板。
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:@。gif] owner:self];
[pboard setData:data forType:@。gif];
return YES;
}

- (NSDragOperation)tableView:(NSTableView *)tv validateDrop:(id< NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op
{
//在此处添加代码以验证删除

if(row> [m_imageArray count])
return NSDragOperationNone;

if(nil == [info draggingSource])//从其他应用程序
{
return NSDragOperationNone;
}
else if(self == [info draggingSource])// From self
{
return NSDragOperationNone;
}
else //从其他文档
{
[tv setDropRow:row dropOperation:NSTableViewDropAbove];
return NSDragOperationCopy;
}

NSLog(@validate Drop);
return NSDragOperationCopy;



- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id< NSDraggingInfo>)info
row:(NSInteger)row dropOperation: (NSTableViewDropOperation)操作
{
NSPasteboard * pboard = [info draggingPasteboard];
NSData * rowData = [pboard dataForType:@。gif];
NSIndexSet * rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSInteger dragRow = [rowIndexes firstIndex];

//将指定的行移动到新位置...
}


registerForDraggedTypes:。否则,你已经注意到,这些方法都不会被调用。


Can anyone help me to implement drag and drop in an NSTableView? I used this code below, but these methods are not getting called during execution.

- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
    // Copy the row numbers to the pasteboard.
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
    [pboard declareTypes:[NSArray arrayWithObject:@".gif"] owner:self];
    [pboard setData:data forType:@".gif"];
    return YES;
}

- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op
{
    // Add code here to validate the drop

    if (row > [ m_imageArray count])
        return NSDragOperationNone;

    if (nil == [info draggingSource]) // From other application
    {
        return NSDragOperationNone;
    }
    else if (self == [info draggingSource]) // From self
    {
        return NSDragOperationNone;
    }
    else // From other documents
    {
        [tv setDropRow: row dropOperation: NSTableViewDropAbove];
        return NSDragOperationCopy;
    }

    NSLog(@"validate Drop");
    return NSDragOperationCopy;
}


- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
              row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard* pboard = [info draggingPasteboard];
    NSData* rowData = [pboard dataForType:@".gif"];
    NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
    NSInteger dragRow = [rowIndexes firstIndex];

    // Move the specified row to its new location...
}
解决方案

You need to declare a custom drag type for your table view and then call registerForDraggedTypes: with your custom type. Otherwise, as you have noticed, none of these methods will get called.

这篇关于在NSTableView中实现拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:54