我是Cocoa dev的新手,但在我的NSTableView中实现单行拖放操作相当简单。但是,当我选择多行时,我现在很难让它正常工作。
当所有选定的行都是连续的时,它似乎没有错误,但是当它例如选择第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;
}
答案 0 :(得分:4)
所以经过多次拖放(以及那里混合了一些应用程序崩溃)后,我又开始进行网络搜索found this class on GitHub。
我在那里模仿了代码,现在它的工作就像一个魅力。