Retrieving the selected object in an NSOutlineView - objective-c

I am looking to retrieve the selected object in an NSOutlineView so I can see if the selected object has any children. How would I do this?

NSOutlineView inherits the -selectedRow method from NSTableView. I suspect though, that what you really want is the current selection from whatever NSController is acting as your outline view's data source.

Related

Detect which NSTableView is active

I have an NSSplitView showing two NSTableView instances. I need to detect which table view has become "active" (of focused), which means the one that the user has clicked. I need to know that because each table view acts as a source list for another view that shows the content of the selected row(s). This other view is shared for both tables.
I could do it by subclassing NSTableView and reacting to mouseDown: or another method but it I'd rather avoid subclassing just for that. I also don't want to track any NSWindow event just to know if the user has clicked one of the tables (I'd rather subclass NSTableView).
Currently, I use the delegate method tableViewSelectionDidChange:, but this method is, obviously, only called when the selected row changes. I need to know that a table becomes active even if the selected row hasn't changed.
Observing the clickedRow property of the table views doesn't appear to work. If may not be KVO compliant.
Any ideas?
For those interested, the most convenient solution I found was to take advantage of the fact that NSTableView is a subclass of NSControl. So just like NSButton it can send action messages when clicked (upon mouse up).
For each tableView, I wired its "action" to the same ibaction selector of my controller object in interface builder.
The controller identifies the sender and acts accordingly.
No need to subclass NSTableView.

Cocoa- Using representedObject for NSButton in NSCollectionView

Setup
I have a NSCollectionView. I have a checkbox in the View Prototype. I've successfully set up bindings so the Card Title and action get populated. (image 1, below)
Goal
I'd like, when I click the checkbox, to run a function that accesses the specific CardModel that the View Prototype is already able to access. I'll then manipulate its data accordingly.
Research
I found this article on SO: Get the representedObject values of NSCollectionViewItem NSButton click, which describes my situation pretty well. The answer, unfortunately, is without specific code. Here's what's suggested:
So, first, set the represented object of your button's cell to the
collection view item that owns the button. (You can do this in the nib
editor.) Then, in your action method, get the button's cell, then the
cell's represented object (which is the item), then the item's
represented object.
Seems simple enough, right?
Attempted Solution(s)
I create a method cardCheckBoxClicked: and connect it to the checkbox.
As per the advice above, I connect the button cell's outlet representedObject to Card Collection View Item. (image 2)
I then attempt to get the Card Collection View Item's representedObject in code.
From MainWindowController.h:
-(IBAction)cardCheckBoxClicked:(id)sender
{
CardModel* cModel = [[sender representedObject] representedObject];
NSLog(#"card title: %#",cModel.title);
}
Error
When I click on the checkbox, I get the following error:
-[NSButton representedObject]: unrecognized selector sent to instance 0x6080001581b0
Question!
So - how do I access the button cell's represented object? Did I misunderstand the advice given above? How can I successfully access the data I need?
Images (reference)
bindings example
represented object connection
This here:
-[NSButton representedObject]:
Is you asking the Class NSButton to run the method representedObject. Make sure you distinguish between a Class an an object or instance of that class.
You need to take the actual button, get its button cell, (at least I think that's what you want), and then call representedObject on the cell. If I am understanding you correctly. I never touch interface builder, so here's completely made up code that lines up with what you are asking for.
someObject = [[theButton cell] representedObject];
In addition to CH Buckingham's answer, you should also consider using bindings. You can bind the checkbox's value binding to the collection view item with a model key path which goes through representedObject to some property on your CardModel. (If desired, the key path can keep going through your model object graph.) That will set that property whenever the button is toggled.

How to lock parent rows in NSOutlineView populated by NSTreeController

I have an NSOutlineView that is populated from an array via an NSTreeController. I would like to make it so that rows that have children are not editable and popups in those rows are hidden. You can see my outlineview in the attached screenshot – it's essentially a tree of settings.
How can I go about this or do I need to implement the datasource via a delegate instead of using the NSTreeController to do it?
Have you looked into the delegate methods?
Specifically interesting are
– outlineView:shouldEditTableColumn:item:
which allows to determine if an item can be edited. Just check for children, if item is an parent and return NO;
Hiding the popup buttons depends on how you have setup your NSOutlineView. In a cell-based outline view use for example
– outlineView:willDisplayCell:forTableColumn:item:
to hide cell content for parent rows. In a view based outline view another method comes into play to customize certain columns:
– outlineView:viewForTableColumn:item:
This should be easy to use to achieve your results.

NSArrayController selection not updating with NSTableView binding

This has been befuddling me for hours...
I have an object (CuesDoc) with a property (NSMutableArray *cuesArray) and some other properties. The cues array holds multiple Cue objects. There is a property called (CuesDoc*) currentCuesDoc in my AppDelegate.
In IB, I have an NSArrayController, which is bound to the AppDelegate.currentCuesDoc.cuesArray.
I have a view-based NSTableView which is bound to the NSArrayController and can add/remove/edit values in the table and cuesArray. So far so good.
I have detail fields below that, which are bound to NSArrayController.selection, with the model key path set to each property.
When the view first appears, the detail fields populate with the contents of the first item in the table view, however when I select other rows, the detail fields do not update to reflect the current selection.
I added an observer to selectionIndexes and selection for the NSArrayController, and when the view appears, I get called for the observeValueForKeyPath: method once, but not after changing selections.
For view-based NSTableViews, you must bind the tableView's selection indexes to the array controller key of selectionIndexes to keep the view's selection in sync with the controller's selection. Selection bindings are separate from content bindings. The older, cell-based NSTableView APIs did not require this step.

When using a view-based NSOutlineView (source list) using bindings, is it possible to not display rows from the data source based on some condition?

For example the image below shows an NSOutlineView bound to a tree structure based on folders and items using an NSTreeController:
What I want is the Item objects to remain in the model, but not to be displayed as rows, i.e:
The NSOutlineView delegate protocol has a method that informs the delegate that an item is about to be displayed, but doesn't give the option not to display it.
Is there some way in which to subclass NSOutlineView to implement this (or some other method)?
Thanks.
Presumably you're using NSTreeController which organizes your model objects according to the key path they use to identify their children.
If you want to filter anything out of the view, all you need to do is implement your child key path method to only return the children you want to display. (If you need to continue keeping track of the "real" children in your model, this may mean some extra bookkeeping to be able to return a separate list of children for display.)