我对 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;
}