Laurent Crivello
Laurent Crivello

Reputation: 3931

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

Upvotes: 1

Views: 314

Answers (2)

Srinidhi
Srinidhi

Reputation: 729

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.

Upvotes: 2

Cory
Cory

Reputation: 2312

Your NSOutlineView "Controller" class is actually a subclass of NSOutlineView, this is different then the NSOutLineView in your XIB file. If you look at the notification object being sent it is an instance of NSOutlineView, not "OutlineViewController", therefore you are calling selectedRow on an incorrect instance.

This code should be place in a subclass of NSViewController as opposed to NSOutlineView. Then create an outlet from the outlineView to the controller.

Upvotes: 1

Related Questions