I have a simple Core Data model with just one entity which has two relationships, parent and children, with them pointing at each other and parent being to many children. They also have three attributes, index, an Int 64, name, a string, and type, an Int 16.
This model is bound to an Array Controller, a Tree Controller, and a custom class that serves as an NSOutlineViewDataSource.
In the header I have this:
In main I have these methods:
My issue is this: I can't rearrange items within a given tier. I can move them into and out of each other, but I can't simply have 3 items all on the same level and move the bottom one to be at the top, for example. If I try, what happens is I get a log message that properly reports where I moved the item to and from, but it doesn't actually move the item until I quit the app and relaunch it. Moving items into and out of eachother is instantly reflected. Any idea what I'm doing wrong?
I followed the tutorial I found here to get most of the way to where I am:
http://lifeasclay.wordpress.com/201...e-data-in-10-6-part-1-ordered-trees/#more-442
This model is bound to an Array Controller, a Tree Controller, and a custom class that serves as an NSOutlineViewDataSource.
In the header I have this:
Code:
@interface OutlineViewController : NSObject <NSOutlineViewDataSource> {
IBOutlet NSTreeController* treeController;
IBOutlet NSOutlineView* outlineView;
NSArray* dragType;
NSTreeNode* draggedNode;
}
In main I have these methods:
Code:
- (void)awakeFromNib {
dragType = [NSArray arrayWithObject:@"factorialDragType"];
[outlineView registerForDraggedTypes:dragType];
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES];
[treeController setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}
- (BOOL)outlineView:(NSOutlineView*)outlineView writeItems:(NSArray*)items toPasteboard:(NSPasteboard *)pasteboard {
[pasteboard declareTypes:dragType owner:self];
draggedNode = [items objectAtIndex:0];
return YES;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index {
NSManagedObject* draggedTreeNode = [draggedNode representedObject];
[draggedTreeNode setValue:[item representedObject] forKey:@"parent"];
NSLog(@"Item dragged from %l to %li", [[draggedTreeNode valueForKey:@"index"] longValue], index);
[draggedTreeNode setValue:[NSNumber numberWithLong:index] forKey:@"index"];
[outlineView reloadData];
return YES;
}
- (BOOL)category:(NSManagedObject*)category isSubCategoryOf:(NSManagedObject*)possibleSub {
if (category == possibleSub) {
return YES;
}
NSManagedObject* possibleSubParent = [possibleSub valueForKey:@"parent"];
while (possibleSubParent != NULL) {
if (possibleSubParent == category) {
return YES;
}
possibleSubParent = [possibleSubParent valueForKey:@"parent"];
}
return NO;
}
- (id)outlineView:(NSOutlineView*)outlineView objectValueForTableColumn:(NSTableColumn*)tableColumn byItem:(id)item {
return NULL;
}
- (NSDragOperation)outlineView:(NSOutlineView*)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index {
// TODO: Don't propose moving it to where it already is.
if ([item representedObject] == NULL) {
return NSDragOperationGeneric;
}
if ([self category:[draggedNode representedObject] isSubCategoryOf:[item representedObject]]) {
return NO;
}
return NSDragOperationGeneric;
}
-(NSInteger)outlineView:(NSOutlineView*)outlineView {
return 1;
}
-(BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item {
return YES;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return NULL;
}
My issue is this: I can't rearrange items within a given tier. I can move them into and out of each other, but I can't simply have 3 items all on the same level and move the bottom one to be at the top, for example. If I try, what happens is I get a log message that properly reports where I moved the item to and from, but it doesn't actually move the item until I quit the app and relaunch it. Moving items into and out of eachother is instantly reflected. Any idea what I'm doing wrong?
I followed the tutorial I found here to get most of the way to where I am:
http://lifeasclay.wordpress.com/201...e-data-in-10-6-part-1-ordered-trees/#more-442