Created
March 12, 2011 01:21
-
-
Save mikeabdullah/866907 to your computer and use it in GitHub Desktop.
How to fix node insertion in NSTreeController on Leopard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5 | |
- (void)didInsertNode:(NSTreeNode *)node; | |
{ | |
// See if model matches tree state. Leopard gets them out of sync, adding to the model rather than inserting | |
id object = [node representedObject]; | |
NSIndexPath *indexPath = [node indexPath]; | |
NSUInteger index = [indexPath lastIndex]; | |
NSTreeNode *parent = [node parentNode]; | |
NSMutableArray *objects = [[parent representedObject] mutableArrayValueForKeyPath: | |
[self childrenKeyPathForNode:parent]]; | |
if ([objects objectAtIndex:index] != object) | |
{ | |
// Correct model object's index | |
[objects removeObjectIdenticalTo:object]; | |
[objects insertObject:object atIndex:index]; | |
} | |
} | |
- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath; | |
{ | |
[super insertObject:object atArrangedObjectIndexPath:indexPath]; | |
NSTreeNode *node = [[self arrangedObjects] descendantNodeAtIndexPath:indexPath]; | |
OBASSERT([node representedObject] == object); | |
[self didInsertNode:node]; | |
// Correcting the model may have lost selection | |
if ([self selectsInsertedObjects]) [self setSelectionIndexPath:indexPath]; | |
} | |
- (void)moveNodes:(NSArray *)nodes toIndexPath:(NSIndexPath *)startingIndexPath; | |
{ | |
[super moveNodes:nodes toIndexPath:startingIndexPath]; | |
NSArray *selection = [self selectionIndexPaths]; | |
for (NSTreeNode *aNode in nodes) | |
{ | |
[self didInsertNode:aNode]; | |
} | |
[self setSelectionIndexPaths:selection]; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment