我对 Cocoa 开发相当陌生,但是在我的 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;
}
最佳答案
因此,经过多次拖放(以及一些应用程序崩溃)后,我再次开始网络搜索,found this class on GitHub .
我模仿了那里的代码,现在它的工作就像一个魅力。
关于objective-c - 如何处理 NSTableView 多行拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10420671/