Skip to content

Instantly share code, notes, and snippets.

@mikeabdullah
Created March 12, 2011 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeabdullah/866907 to your computer and use it in GitHub Desktop.
Save mikeabdullah/866907 to your computer and use it in GitHub Desktop.
How to fix node insertion in NSTreeController on Leopard
#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