问题描述
我对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 {
while(dragRow!= NSNotFound)
{
int putRow = row +
int deleteRow = dragRow;
if(putRow> [_filePaths count]){putRow = [_filePaths count]; }
if(i> = 1)
{
//已经删除的行的偏移量修正
deleteRow = deleteRow -
}
[_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;
}
(和几个应用程序崩溃混合在那里)我开始网络搜索,并再次找到
我模仿了那里的代码,现在它的工作方式类似于charm。
I'm fairly new to Cocoa dev, but implementing single row drag and drop within my NSTableView was fairly straightforward. However I'm now having a hard time getting it to work right when multiple rows are selected.
It seems to work without fault when all the selected rows are sequential, but it fails when, for example, you select rows 0 and 4 (where 4 is the last row) and drag them to somewhere in between (such as row 1 or 2) — by fail I mean that the wrong row is deleted and so I end up with duplicates.
Here is my acceptDrop
code so far:
- (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;
}
So after much dragging and dropping (and a few application crashes mixed in there) I started web searching again and found this class on GitHub.
I mimicked the code there and now it's working like a charm.
这篇关于如何处理NSTableView拖放多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!