Search code examples
cocoainterface-buildernstableviewselecteditemnsoutlineview

SelectedRow of NSOutlineView always returns -1


I have a View-based NSOutlineView, and have in the class a selection change event:

- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
    NSLog(@"Selected Row inside:%ld",[self selectedRow]);
}

This is the way I create my NSOutlineView:

ovc = [[OutlineViewController alloc] init];
[myOutlineView setDelegate:(id<NSOutlineViewDelegate>)ovc];
[myOutlineView setDataSource:(id<NSOutlineViewDataSource>)ovc];

MyOutlineView is created in IB.
Every time I click on a row, the event gets fired, however the result is always -1.

NSLog(@"Item 0:%@",[self viewAtColumn:1 row:0 makeIfNecessary:YES]);

Always returns NULL.

Is there something specific I should do ? Thanks.

=== EDIT ===
I have published my simplified code showing the issue: http://www.petits-suisses.ch/OutlineView.zip


Solution

  • Instead of checking the selectedRow of self object, which is just a object initialized in AppController which is a wrong instance. You need to check on the notification object as shown below.

    NSLog(@"Selected Row:%ld",[[notification object] selectedRow]);
    

    Also clickedRow is meaningful in the target’s implementation of the action. So the clickedRow gives correct value if checked inside a Action or DoubleAction method.