【发布时间】: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