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

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • do you have better luck if you set "`ovc`" to be a `strong` property of the parent view controller? – Michael Dautermann Feb 04 '15 at 21:40
  • I just tried to add this in the parent view controller: `@property (retain,strong) NSOutlineView *ovc;`, but unfortunately same issue. – Laurent Crivello Feb 04 '15 at 21:56
  • And does the row actually select in the UI? – Michael Dautermann Feb 04 '15 at 22:58
  • Please check your outline view selection is disabled or not . You can refer this http://stackoverflow.com/questions/7285417/nstableview-disable-row-selection . By the way you can access the clickedRow property. – Sheen Vempeny Feb 05 '15 at 06:22
  • Yes the row actually selects in the UI (gets dark blue background). – Laurent Crivello Feb 05 '15 at 07:51
  • I have no specific code to disable selection on the OutlineView. Even checked in the IB, but nothing is going into that direction. clickedRow returns -1 too. By the way it's a view-based outline view, not sure if this can be of any influence. – Laurent Crivello Feb 05 '15 at 07:56
  • FYI I have added to my question a link to the code so you can see the issue. – Laurent Crivello Feb 05 '15 at 08:44
  • Oh, good point. I have linked the XIB's view controller with my viewcontroller IBOutlet, and changed it's class to my Outline view controller. However still the same issue. I have republished the updated zip. I reused one of the few samples found in the internet. How should I perform the inheritance ? Thanks ! – Laurent Crivello Feb 05 '15 at 12:15
  • I checked the code . The problem is with you controller class inheritance.Its inherited from NSOutlineView. When you access selectedRow, it gives the controller object selectedRow not from the outline view presented in XIB. Just inherit the controller class from NSObject. – Sheen Vempeny Feb 05 '15 at 12:17
  • _italic_ **bold** `@interface OutlineViewController : NSOutlineView { NSMutableArray *list; NSOutlineView *listView; } @property(nonatomic,retain)NSOutlineView *listView; @end` – Sheen Vempeny Feb 05 '15 at 12:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70302/discussion-between-sheen-vempeny-and-laurent-crivello). – Sheen Vempeny Feb 05 '15 at 12:24

2 Answers2

2

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.

Srinidhi
  • 719
  • 5
  • 8
1

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.

Cory
  • 2,302
  • 20
  • 33