|
|
#1 |
|
Allow NSOutlineView to be rearranged?
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: Code:
@interface OutlineViewController : NSObject <NSOutlineViewDataSource> {
IBOutlet NSTreeController* treeController;
IBOutlet NSOutlineView* outlineView;
NSArray* dragType;
NSTreeNode* draggedNode;
}
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;
}
I followed the tutorial I found here to get most of the way to where I am: http://lifeasclay.wordpress.com/2011...rees/#more-442
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
|
|
|
0
|
|
|
#2 |
|
I'm not a big fan of doing this but I'm still stuck with this... Bump?
Does anyone know what I'm missing? Why does the row go back where it started when I drag and drop it (and don't move the tier it's in.) Edit: I've finally found a promising method: Code:
[treeController moveNode:(NSTreeNode*) toIndexPath:(NSIndexPath*)]; Last edited by ArtOfWarfare; Dec 5, 2012 at 10:12 PM. |
|
|
|
0
|
|
|
#3 |
|
Alright... so now I have this code:
Code:
- (BOOL)outlineView:(NSOutlineView *)mOutlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)parent childIndex:(NSInteger)index {
[draggedObject setValue:[parent representedObject] forKey:@"parent"];
NSInteger dropIndex = (index == NSOutlineViewDropOnItemIndex) ? 0 : index;
[draggedObject setValue:[NSNumber numberWithLong:index] forKey:@"index"];
NSIndexPath* dropPath = (parent) ? [[parent indexPath] indexPathByAddingIndex:dropIndex] : [NSIndexPath indexPathWithIndex:dropIndex];
[treeController moveNode:draggedNode toIndexPath:dropPath];
return YES;
}
This is confusing/frustrating to say the least...
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 03:53 PM.






Linear Mode
