I have a master-detail GUI layout:
NSTableView with content bound to an NSArrayController.
The NSArrayController data is bound to NSUserDefaults rows.
An NSTextField with value bound to the NSArrayController's selection property.
The table is populated with data from the storage as it should, and selecting a row changes the textField value.
Editing the textfield changes the corresponding table row as it should, but it does not change the NSUserDefaults!
If I relaunch the app, the changes are not saved.
BTW, if I add/remove rows from the table - it is persisted, but changes to row data are not.
It seems like the changes in the NSArrayController selection dictionary are not propagated to NSUserDefaults.
What solved the problem finally was checking "Handles Content as Compound Value" in the content binding preferences of the ArrayController.
Screenshot:
Related
I have an NSButtonCell as data cell in one of my NSTableView's columns. The table view is cell-based and has two columns ; one with the button cell, one with a text field cell.
The button cell's respective column's target and argument are bound to my app delegate (as the target) and to the selectedObjects array of an NSArrayController (as the argument).
Basically the same as what's outlined in one of Apple's Technical Q&As.
When I open the app, the table view is automatically populated. Everything works as expected. If I click a button my action method gets called just fine, and this method receives the selectedObjects as a parameter.
It's just not the functionality I want ; I'd like to get the object that is represented by the row of the clicked NSButtonCell instead of the currently selected objects (in my case this is an NSString as my NSArrayController is populated with strings).
Is it possible to get this particular object, only through bindings? Or should I, instead, set the selector of the NSButtonCell (the data cell) without bindings? And then get the object by using NSTableView's -clickedRow method?
I have a program using Core Data, with the columns of an NSTableView bound through an NSArrayController. This is working with out an issue, table columns populate without issue. I am expanding the table to include an NSPopupButton - however something odd happens when the button is added. Just dragging the button in to the column, then running the program without connections or binding to the Popup, all the other columns do not populate correctly - they just show the default placeholder text.
Thoughts?
I can get the NSPopupButton to populate, but still the other columns only show the default text. If I remove the NSPopupButton everything works fine.
Why would the other columns have issues? I tried added a CheckBox and had the same issue. I've searched around the net and SO, but have not found an answer.
I would appreciate any thoughts/suggestions.
Thank you in advance.
[EDIT]----
The Table is view-based and NSTableCellView.
arrayController:
Bound to Entity Name
Parameters, bound to Main Controller; key path:managedObjectContext
TableView:
Columns:
Value: Bound to arrayController, key path: name, age, etc...
NSPopupButton:
Value: Bound to Main Controller, Path: catArrayController.arrangedObjects.name
Main Controller:
IBOutlet: catArrayController
Bound to catArrayController in IB
With a view-based table view, you are intended to bind the table view's Content binding to the array controller. You do not bind the columns' Value binding. From Table View Programming Guide for Mac: Populating a Table View Using Cocoa Bindings:
Note: In an NSView-based table, you bind the table view’s content to the array controller’s arrangedObjects. This differs from the technique you use if you’re working with an NSCell-based table.
That binding causes each cell view to have its objectValue property set to the object corresponding to the row, if it has such a property. Note that the cell views don't get a column-specific value. All of the cells of a row get the same value.
Since your cell views are NSTableCellViews, they have an objectValue property. Then, the subviews of the cell view should have their Value binding bound to the cell view with a key path running through objectValue to the specific property (name, age, etc.).
In the old NSCell-based table views, you would typically not bind the Content (or Selection Indexes or Sort Descriptors) bindings of the table view itself at design time. You would bind the table columns as you have done. At run time, the table view would automatically establish bindings for its own Content, Selection Indexes, and Sort Descriptors based on the bindings of its columns. I suspect some version of this behavior was happening in your app which was allowing things to kind of work. However, I suspect that adding the new column broke that system. It was a fluke that it was working for the view-based table view in the first place, so was inherently fragile.
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.
I want to press a button, and have a specific box in my NSTableView change its value to something else. Is that possible? How can I do this?
Change the model.
That is, get the object you want to change from your array controller, and then modify its attributes.
If you're using a binding, the table will automatically reload the changed cell. If you're using the table view data source methods, just ask the table view to reload the changed cell using -reloadDataForRowIndexes:columnIndexes:.
I have a NSTableView and each row has a NSComboBox.
The table column is bounded to NSArrayControllerA and each NSComboBox is bounded to NSArrayControllerB.
I would like to be notified when the selected value in any NSComboBox changes.
So far, I've tried to add a listener to the NSArray managed by the NSArrayControllerB, but I'm not notified about any change.
I've also tried to add an observer to the NSTableView, with a NSComboBoxSelectionDidChangeNotification but it seems the notifications are not propagated to the parent views. And the combo boxes are dynamically created when a new row is inserted.
thanks
UPDATE: How is the NSComboBox bound ?
OK. I assume you've bound the combo box's selection to the array controller's selection? If so, try observing the array controller's selectionIndex property. (It's KVO-compliant.)
On my iPhone so I can't easily test right now, but that should do the trick.
OK, scratch that, now that I better understand the question.
How about instead you set the selector for the cell to some method in your controller (with an outlet to the enclosing table view), say, -comboBoxClicked: and then implement something like:
- (void)comboBoxClicked:(id)sender
{
NSUInteger changedRow = [[self tableView] selectedRow];
// Do something with changedRow
}
I did a cursory test (just NSLogging changedRow) and it seemed to work for me, at least in a very basic application.