2010-12-12 4 views
2

내 응용 프로그램의 두 테이블보기 사이에 NSManagedObject에 대한 참조를 드래그해야합니다. NSManagedObject에 대한 참조를 저장하는 기본 NSPasteboard 유형은 무엇입니까?NSManagedObject에 대한 NSPasteboard 유형

현재 해결책은 NSPasteboardTypeString에 개체의 NSManagedObjectID URIRepresentation을 저장하는 것입니다. 거기에 더 우아한 해결책이 있다고 생각합니다.

답변

3

모델 개체와 그 처리 방법이 응용 프로그램마다 고유하므로 모든 모델 개체에 표준 형식이 없습니다. 모든 경우에 대해 하나의 마분지 유형이 있다면 그것들을 구분하지 않을 것입니다. 사용자 정의 객체에는 자체 드래그 유형이 있어야합니다.

"com.yourcompany.yourapp.yourobjecttype"으로 해석되는 "MyObjectPboardType"과 같이 의미가있는 문자열 (Xcode의 자동 완성으로 찾을 수있는 #define)을 사용하십시오.

NSPasteboard의 -declareTypes : owner :를 사용하여 새 유형을 선언 한 다음 -setString : forType : 또는 다른 -set? : forType : 메소드 중 하나를 사용하여 객체 유형에 대한 정보를 설정하십시오. 귀하의 경우, 객체 ID의 사용은 완벽하게 수용 가능한 식별자입니다. 관리 객체의 객체 ID가 새 객체 대 지속 객체 일 때 변경된다는 점을 기억하십시오.

1

동일한 응용 프로그램에서 테이블 내에서 드래그하는 경우 tableView (outlineView)에있는 객체의 rowIndexes (outlineView에서 드래그하는 경우 indexPaths)를 붙여 넣을 수 있습니다. 이것은 tableViews의 dataSource가 NSArrayController (outlineView의 경우 NSTreeController) 인 경우 불필요한 CoreData 액세스에서 벗어날 수 있습니다. 그러면 'tableView : validateDrop : proposedRow : proposedDropOperation :'메서드와 'tableView : acceptDrop : row : dropOperation :'메서드에 전달 된 'info'개체가 참조로 포함되므로 드롭을 허용 할 때 끌어온 개체를 쉽게 검색 할 수 있습니다. 'draggingSource'키 경로 아래에서 드래그를 시작한 tableView

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 
} 
: 여기

은 간단한 구현입니다
관련 문제