Question

This is probably just lack of experience with the NSOutlineView but I can't see a way to do this. I have a NSOutlineView (implemented with the excellent PXSourceList) with an add button that is totally functional in the aspect that I save/write/insert/delete rows correctly. I do not use a NSTreeController, and I don't use bindings. I add the entity using the following code:

- (void)addEntity:(NSNotification *)notification {
    // Create the core data representation, and add it as a child to the parent node
    UABaseNode *node = [[UAModelController defaultModelController] createBaseNode];
    [sourceList reloadData];
    for (int i = 0; i < [sourceList numberOfRows]; i++) {
        if (node == [sourceList itemAtRow:i]) {
            [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:i] byExtendingSelection:NO];
            [sourceList editColumn:0 row:i withEvent:nil select:NO];
            break;
        }
    }
}

When the add button is pressed, a new row is inserted like this:

enter image description here

If I click away, then select the row and press enter to edit it, it now looks like this: enter image description here

My question is: How can I programmatically get the same state (focus, selected, highlighted) the first time, to make the user experience better?

Was it helpful?

Solution

Something like this works for me:

- (void)addEntity:(NSNotification *)notification {
    // Create the core data representation, and add it as a child to the parent node
    UABaseNode *node = [[UAModelController defaultModelController] createBaseNode];
    [sourceList noteNumberOfRowsChanged];
    NSInteger row = [sourceList rowForItem:node];
    [sourceList scrollRowToVisible:row];
    [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
    [sourceList editColumn:0 row:row withEvent:nil select:YES];
}

You can use rowForItem: instead of repeatedly checking itemAtRow:.

You generally also want to use [sourceList scrollRowToVisible:...] in case the new row isn't visible and you can use noteNumberOfRowsChanged instead of reloadData, unless the data has actually changed.

The standard Mac behavior is to select the contents of a newly created item, so use select:YES.

If that doesn't help, there's something else going on in your code which the above snippet doesn't communicate...

In general, I'd really suggest when learning a new class that you read all the way through the documentation page listing the methods available (excepting the deprecated methods), or at the very least all the methods available for the task you're trying to perform; you'll get a much better idea of the capabilities of the class and be less likely to use an inappropriate/inefficient/inelegant method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top