【问题标题】:How to handle NSTableView Drag and Drop for multiple rows如何处理多行的 NSTableView 拖放
【发布时间】:2012-05-02 19:51:50
【问题描述】:

我是 Cocoa 开发的新手,但是在我的 NSTableView 中实现单行拖放非常简单。 However I'm now having a hard time getting it to work right when multiple rows are selected.

当所有选定的行都是连续的时,它似乎可以正常工作,但是当您选择第 0 行和第 4 行(其中 4 是最后一行)并将它们拖到中间的某个位置(例如行1 或 2) — 失败是指删除了错误的行,所以我最终得到了重复。

到目前为止,这是我的acceptDrop 代码:

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
        row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard* pboard = [info draggingPasteboard];
    NSData* rowData = [pboard dataForType:BasicTableViewDragAndDropDataType];
    NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
    NSInteger dragRow = [rowIndexes firstIndex];
    NSArray *tempArray = [[NSArray alloc] initWithArray:_filePaths copyItems:YES];

    int i = 0;

  if (dragRow < row) 
    {
        while (dragRow != NSNotFound) 
        {
            int putRow = row + i;
            int deleteRow = dragRow;

            if (putRow > [_filePaths count]) { putRow = [_filePaths count]; }
            if (i >= 1)
            {
                // offset fix for already deleted rows
                deleteRow = deleteRow - i;
            }

            [_filePaths insertObject:[tempArray objectAtIndex:dragRow] atIndex:putRow];
            [_filePaths removeObjectAtIndex:deleteRow];

            dragRow = [rowIndexes indexGreaterThanIndex:dragRow];

            i++;
        }

        [_imagesTableView noteNumberOfRowsChanged];
        [_imagesTableView reloadData];

    return YES;

  }

    NSLog(@"dragging up");

    while (dragRow != NSNotFound) 
    {
        int putRow = row + i;
        if (putRow > [_filePaths count]) { putRow = [_filePaths count]; }

        [_filePaths removeObjectAtIndex:dragRow];
        [_filePaths insertObject:[tempArray objectAtIndex:dragRow] atIndex:putRow];

        dragRow = [rowIndexes indexGreaterThanIndex:dragRow];

        i++;
    } 

    [_imagesTableView noteNumberOfRowsChanged];
    [_imagesTableView reloadData];

  return YES;
}    

【问题讨论】:

    标签: objective-c cocoa nstableview


    【解决方案1】:

    因此,在多次拖放(以及一些应用程序崩溃混在一起)之后,我再次开始网络搜索并found this class on GitHub

    我在那里模仿了代码,现在它就像一个魅力一样工作。

    【讨论】:

      猜你喜欢
      • 2010-10-13
      • 2012-05-05
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多