Edit specific row and column of NSTableView programmatically - objective-c

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:.

Related

NSPopupButton not working in an NSTableview

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.

ArrayController's CoreData selection binding not refreshed across multiple NIB files

I've a hard time getting my Cocoa application to work as expected. It consists of a toolbar in the main.nib and a custom view in a details.nib file. Now I want the user to select an entry in a NSPopupButton in the toolbar and the content of the custom view should be changed accordingly.
To achive this I've added an ArrayController to my main.nib, showing the following configuration:
Furthermore the Managed Object Context is bound to the Model Key Path delegate.managedObjectContext (it is no document based application).
With this configuration the NSPopupButton works just fine and if I add a Label to the toolbar (also in the main.nib) and bind it's value to the selection (Controller Key), name (Key Value Path) the content is refreshed whenever I change the selection.
The Bindings of the NSPopupButton look like shown in the following screenshot:
So in my details.nib I tried the following to achieve the same effect. I've added an ArrayController, whichs Managed Object Context is also bound to the Model Key Path delegate.managedObjectContext. Also the configuration is exactly the same as shown in the above pictures. I've then added a label and bound it's value to the selection (Controller Key), name (Key Value Path) of this ArrayController.
The problem is that the Label only displays the the initial selection after the application did launched correctly. Afterwards, when I change the selection of my NSPopupButton, the label does not change accordingly.
What are my options to get the ArrayController working accross multiple NIB files?
BTW: I've tried to follow this blog post to get it working but it seems I'm missing something here.
Update:
If I replace the Label in the details.nib by a NSTextField and change the text of it, the changes are reflected in the related NSPopupButton entry. So I guess I made something right, but the main problem remains: I can only edit the entry which was loaded during application startup. Switching to another NSPopupButton entry does not change the text in the NSTextField.
Update 2:
I've created a small sample project with exactly the same configuration and uploaded it on GitHub. So feel free to check it out or create a pull request with a solution approach.
It seems you're missing the fact that, when you create the second array controller on the Details.xib it has no relation to the array controller on the MainMenu.xib. They are two separate instances.
When you change the selection on the PopUp the only array controller affected is the one on MainMenu.xib.
You have several options here:
When you create your DetailViewController pass a reference to the array controller on the Controller and bind to that (don't create a new one on the details.xib)
Just use simple KVO to observe the selection on Controller, and programatically change your label value.
Just use simple KVO to observe the selection on Controller and update the array controller on the DetailsViewController to keep them in sync.
your solution here...
As long as you understand what's going on I'm sure you'll find the best solution to your original problem.

Cocoa Bindings "Selection" not changing when table rows clicked

Hello there: I have in essence a master-detail application with one master view-based NSTable view and two detail views. My master NSTable loads and displays data from a Managed Object Context correctly. One of my detail views should load the image of the object in selected table row. But here is my problem that no one else on the Internet seems to have:
My "selection" object isn't changing. I did bind the the value of image view on my detail view to the Array Controller, and controller key is: "selection", model key is "image". The image view only loads the first object's image in the table, and if I click/select on other rows, the image isn't changing. So it seems like "selection" object is always the first object. Since this is all done in Interface Builder, I don't have any code to show, sorry.
Does anyone know what is going on? Why is the "selection" object always the first in the table? Thank you!
You need to bind the NSTableView’s selection to the NSArrayController’s, like so:

Who calls commitEditingStyle:forRowAtIndexPath?

I've got a custom UITableViewCell that allows a user to input text when in edit mode. I've noticed that on stock UITableViewCells, when I swipe left, then hit the Delete button, it's table view receives the commitEditingStyle:forRowAtIndexPath message. I'm curious where this message is coming from. As far as I can tell, individual cells don't have a reference to the table view they belong to. If I want my custom cell to send this message, how would I go about doing that? I've thought about using the superview of the cell, but it seems like there's got to be an easier way.
This method gets called automatically (if you implement the data source protocol) before adding any row or column.
If you want to know to which table view a cell is belonging, use the superview method.

Using Custom Cell for Mac Application

Does anyone have any info on this? I'm new to cocoa, all tutorials seem to be for iPhone which uses a different view controller. Anyone willing to provide a step by step for adding labels to a custom cell? (I'm pulling from Core Data)
EDIT: It's important to note I'm using Core Data here. Many tutorials use arrays.. I don't understand why you would use that??
I'll award an answer quickly!
Zach
I'm not sure if this is going to help you, but your problem seems unrelated to your use of CoreData.
If I've understood your problem correctly, here are some steps:
Populate your NSTableView
Using CoreData, what you can do is put an NSArrayController object in your XIB document, set its mode to Entity and choose the Entity you want to display in your TableView (all of this from the first tab of the inspector on your array controller object).
Then, bind your NSTableView Content to the arrangedObjects of the array controller. You might also bind the selectionIndexes and use some sort descriptors on the array controller to order your data, as CoreData will give you a set rather than an array.
Click on your table view as many times as necessary as to select the table column in which you want your custom cell to appear, and set its Value binding to arrangedObjects too.
Set your custom cell
Finally, click on the cell of this table column and in the "Identity" of the inspector, change the class name of the cell to the class name of your custom cell.
I'll let you read the appropriate documentation to implement your custom cell according to what you want to achieve.
With the different bindings I've described, the objectValue of your cell should be the object from your array controller at the same index of the row your cell will appear on.
Please note that I've not tested again all of these steps but answered from memory... there might be details I've missed but you should have the main steps here.