首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSManagedObject的NSPasteboard类型

NSManagedObject的NSPasteboard类型
EN

Stack Overflow用户
提问于 2010-12-13 06:48:23
回答 2查看 1.5K关注 0票数 2

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

我目前的解决方案是将对象的NSManagedObjectID的URIRepresentation存储在NSPasteboardTypeString中。我怀疑有一个更优雅的解决方案。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-13 08:40:09

所有模型对象都没有标准类型,因为模型对象及其处理方式对于您的应用程序来说是独一无二的。如果只有一种粘贴板类型,那么就不会区分它们。您自己的自定义对象应该有自己的拖动类型。

只需使用一个有意义的字符串(可能是#define,这样您就可以在Xcode中使用自动补全功能找到它),比如解析为"com.yourcompany.yourapp.yourobjecttype“的"MyObjectPboardType”。

使用NSPasteboard的-declareTypes:owner:声明新类型,然后使用-setString:forType:或其他-set?:forType:方法设置对象类型的信息。在您的示例中,使用对象ID是一个完全可接受的标识符。只需记住,托管对象的对象ID在新对象与持久化对象之间会发生变化。

票数 3
EN

Stack Overflow用户

发布于 2017-02-23 20:10:28

如果您在同一个应用程序的表内拖动,则可以将tableView (outlineView)中的对象的rowIndexes (如果是从outlineView拖动,则为indexPaths)放入粘贴板中。如果CoreData的dataSource是NSArrayController (outlineView的NSTreeController),那么这也可以避免一些不必要的tableViews访问。然后,您可以在接受拖放时轻松检索被拖动的对象,因为‘info’对象传递给两个方法‘tableView :validateDrop:proposedRow:proposedDropOperation:’和‘tableView:acceptDrop:row:dropOperation:’将在‘draggingSource’键路径下引用发起拖动的tableView。

下面是一个简单的实现:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4424557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档