2010-12-12 80 views
2

我需要将引用拖到我的应用程序的两个表视图之间的NSManagedObject。什么是首选的NSPasteboard类型来存储对NSManagedObject的引用?NSManagedObject的NSPasteboard类型

我目前的解决方案是将对象的NSManagedObjectID的URIR表示存储在NSPasteboardTypeString中。我怀疑那里有更优雅的解决方案。

回答

3

所有模型对象都没有标准类型,因为您的模型对象以及它们的处理方式对于您的应用程序是唯一的。如果所有人都有一个纸板类型,那么就不会将它们分开。您自己的自定义对象应该有自己的拖动类型。

只需使用一个合理的字符串(也许是一个#define,所以你可以在Xcode中自动完成),就像解析为“com.yourcompany.yourapp.yourobjecttype”的“MyObjectPboardType”一样。

使用NSPasteboard的-declareTypes:owner:声明新的类型,然后使用-setString:forType:或另一个-set?:forType:方法来设置对象类型的信息。在你的情况下,使用对象ID是一个完全可以接受的标识符。只要记住托管对象的对象ID在新的和持久的时候就会改变。

1

如果您在同一个应用程序中的表格内拖动,您可能需要在tableView(outlineView)中的对象上粘贴rowIndexes(indexPaths,以防拖动outlineView)。如果tableViews的dataSource是NSArrayController(用于outlineView的NSTreeController),那么这可能会让您免于某些不必要的CoreData访问。 然后,您可以轻松地检索拖动的对象,因为传递给两个方法'tableView:validateDrop:proposedRow:proposedDropOperation:'和'tableView:acceptDrop:row:dropOperation:'的'info'对象将接受tableView在'draggingSource'键路径下产生拖动。

这里有一个简单的实现:

extern NSString *const kMyLocalDragType = @"com.whatever.localDragType"; 
@implementation MyArrayControllerDataSource 
    . 
    . 
    . 
#pragma mark - NSTableViewDataSource (Drag & Drop) 
+ (NSArray *)dragTypes { 
    // convenience method returning all class's supported dragTypes 
    return @[kMyLocalDragType]; 
} 
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard { 
    [pboard declareTypes:[[self class] dragTypes] owner:self]; 
    for (NSString *aDragType in [[self class] dragTypes]) { 
     if (aDragType == kMyLocalDragType) { 
     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes]; // we are supporting drag&drop of multiple items selected  
     [pboard setData:data forType:aDragType]; 
     } 
     . 
     . // logic for other dragTypes 
     . 
    } 
    return YES; 
} 
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation { 
    NSArray *dragTypes = [info draggingPasteboard] types]; 
    for (id aDragType in dragTypes) { 
     if (aDragType == kMyLocalDragType) { 
     return NSDragOperationCopy; 
     } 
    } 
    . 
    .// Other logic for accepting drops/affect drop operation 
    . 
} 

- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation { 
    if ([info draggingPasteboard] types] containsObject:kMyLocalDragType]) { 
     // Retrieve the index set from the pasteboard: 
     NSData *data = [[info draggingPasteboard] dataForType:kMyLocalDragType]; 
     NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
     NSArray *droppedObjects = [self retrieveFromTableView:tableView objectsAtRows:rowIndexes]; 
     // droppedObjects contains dragged and dropped objects, do what you 
     // need to do with them, then add them to this dataSource: 
     [self.content insertObjects:droppedObjects]; 
     [tableView reloadData]; 
     [tableView deselectAll:nil]; 
     return YES; 
    } 
     . 
     . // other logic for accepting drops of other dragTypes supported. 
     . 
} 

#pragma mark - Helpers 
- (NSArray <NSManagedObject *> *)retrieveFromTableView:(NSTableView *)tableView objectsAtRowIndexes:(NSIndexSet *)rowIndexes { 
    id dataSource = [tableView dataSource]; 
    if ([dataSource respondsToSelector:@selector(content)]) { 
     if ([dataSource.content respondsToSelector:@selector(objectsAtIndexes:)]) { 
      return [datasource content] objectsAtIndexes:rowIndexes]; 
     } 
    } 
    return @[]; //We return an empty array in case introspection check failed 
}