CoreData Binding and custom Cell - cocoa

I have an app with a CoreData Database and a NSTableView.
I want to do a customCell with 3 key values from database.
So I create an NSTextFieldCell Class but the binding is only for a key value.
How I can programmatically bind a NSTableColumn with multiple key value ?
Thanks.

Here's one way to do it which foregoes the need to create a custom cell:
1) In IB, drag over an Array Controller to your Objects sidebar. Under the Attributes Inspector, set its "Mode" to Entity Mode, fill in the "Entity Name" with the name of your entity, and check "Prepares Content". Under the Bindings Inspector, set its "Managed Object Context" to managedObjectContext.
2) In IB, convert your table to be view-based. Select the "Table View" and under the Attributes Inspector, select Content Mode: View Based and change number of columns to 1.
3) In IB, select the "Table Column" and bind its Value to the Array Controller representing your object. Controller Key should be "arrangedObjects" and leave the "Model Key Path" blank.
4) In IB, select the text field (Label) in your NSTableView cell. Bind its Value to the Table Cell View. The "Controller Key" can remain blank and set the "Model Key Path" to objectValue.whateverAttributeName
There's an awesome summary and tutorial of view-based tables at Gentle Bytes.
Tim Isted created a wonderful tutorial on how to programmatically create bindings, if that's specifically what you desire at Blog # Tim Isted.

Related

Problems with cocoa bindings w/ master-detail where the detail contains an array

This is a macOS app, using Core Data, no Documents, and no Storyboards. I'm trying to use NSArrayControllers & bindings to display my data.
The data model 2 entities:
Book (which has attributes like author, title, blurb), and
Chapters (which has attributes like title)
The two entities are connected with a one-to-many relationship:
Book.chapters <—>> Chapter.book.
The app can have multiple books, which it shows in a tableView. Simple enough. I have an Array Controller set up in XCode called "Books Array Controller". I set its Entity as "Book" and check the "prepares content".
It's a master-detail, where one of the properties on the detail is an array to be shown in its own table. And this is what's giving me problems.
I bind the Books Array Controller as follows:
1. The managedObjectContext is bound to Delegate's self.managedObjectContext.
2. The bookListTableView has its Table Content > content bound to "Books Array Controller" controller key = arrangedObjects.
3. And then for each column in the table, I select the textViewCell and bind its value to the "Table Cell View" model key path = objectValue.title, objectValue.author, etc.
4. I have a textView that the "blurb" (attributed string) of the selected book by binding Attributed String to "Books Array Controller" controller key = selection, model key path = blurb.
All of this works just fine.
But I'm having a lot of problems trying to figure out how to display the chapter list in another table. I've tried creating another Array Controller called "Chapters Controller". I set its Entity as "Chapter" and check the "prepares content".
I bind the Chapters Controller as follows:
1. The managedObjectContext is bound to Delegate's self.managedObjectContext. (just like for the other one)
2. The Controller Content > content set is bound to "Books Array Controller" controller key = selection, model key path = chapters.
3. I bind the table's content to the "Chapters Controller" controller key = selection, model key path = chapters.
4. And then for each column in the table, I select the textViewCell and bind its value to the "Table Cell View" model key path = objectValue.title, etc.
Nothing ever shows in that 2nd table, the one for the chapter list. Not even a bunch of "Table View Cell" things. It's empty. I've tried a bunch of variations I won't bore you with. There's clearly something I'm missing. Any help?
The app correctly makes its Book objects, and the Books appear to correctly make their Chapter objects.
———
Update: I've attempted to follow Willeke's advice and am still not doing it right.
Here are the bindings...
Books controller binding:
Chapters Controller binding:
Books TableView binding:
Chapters TableView binding:
The content set of the Chapters array controller is bound to the selection of the Books array controller. When you select a row in the Books table view, the Books array controller's selection has to be synchronized by binding the selections indexes. I usually bind Content to arrangedObjects, Selection Indexes to selectionIndexes and Sort Descriptors to sortDescriptors.
The bindings from the Chapters table view are the same as the Books table view. Bind content of the Chapters table view to arranged objects of the Chapters array controller.

CoreData and the NSComboBox

I have a Mac application that uses CoreData. I have 1 entity "Employee" with 2 attributes, employeeName and employeePin. I have a drawer window that is used to add employees as well. I want an NSComboBox to be populated with the employeeName attribute from CoreData but I can't figure it out. Ive got CoreData working like it should but I can't figure out how to bind the NSComboBox with that particular attribute.
I created an Array Controller and set the parameter "Managed Object Context" to the App Delegate with the model key path managedObjectContext but Im not getting the names to pull up in the combo box. How do i get the names to pull up in the combo box. Any and all help is appreciated.
To fully set up the array controller you also need to set the mode to 'Entity Name' in the identity inspector of the array controller and specify this as 'Employee'
After that you need to bind the 'content value' of the combo box(in bindings inspector) to employeeName of the array controller. (arrangedObjects).
i.e in the Bindings inspector,
click on bind :Array controller
Controller Key :arrangedObjects
Model Key Path : employeeName

NSArrayController , arrangedObjects and tableColumn IB connection

I am new to mac development. I am trying to make this app from apple.com
At one point i am stucked in interface builder.
How do i connect arrangedObjects.firstName to the Value - Table Column - First Name and Last Name?
Edit:
this is my array controller and its bindings.
Thank you in advance..!!
Ctrl Shift click with a mouse on a column in your table (NOT column's header). You should see the list of views which are parent to each other:
Select NSTableColumn view. Then just open bindings tab and perform same procedure as with arrangedObjects' binding.

How to bind the selection of the NSTableView to the NSArrayController

I just want to be able to use the the methods of the nsarraycontroller called remove: and add:
Select the NSTableView. If the inspector window's title shows Scroll View, you need to click the table view again to select it.
In the bindings tab, connect the Selection Indexes binding to the Array Controller's selectionIndexes controller key. This is similar to binding the content to the array controller, except that you don't use the arrangedObjects key.

NSArrayController "Content set" bound to NSTreeController issue

I have an NSOutlineView bound to a NSTreeController and a CoreData Datamodel.
NSOutlineView displays his data properly.
A NSTableView's cell values bound to a NSArrayController, displaying data from CoreData.
NSTableview displays his data properly too.
The Datamodel has a relationship between data for NSOutlineView and NSTableView
When I try to bind NSArrayController's "Content set" to NSTreeController.selection.name
to display all items related to the NSOutlineView selection I get this error:
Cannot create NSSet from object Untitled of class NSCFString
(Where "Untitled" is the value of NSOutlineViews node)
And no data in NSTableView is displayed.
Everything setup in IB - does anyone has a hint for me to get this working?
Thanks a lot!
You want to bind the NSArrayController's Content Set to NSTreeController, it's Controller Key to "selection" and then the Model Key Path should be the relationship name, which I would hope isn't "name". Then in the TableView you bind the column(s)'s value to the NSArrayController, with Controller Key being "arrangedObjects" and Model Key Path the property "name"

Resources