How to show button cell (check box) title in table view, using bindings - objective-c

I am trying a simple application where I have a mutable array of mutable dictionaries, such as -
NSMutableDictionary *sample6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"title6",#"title",[NSNumber numberWithBool:NO],#"state", nil];
In IB I created a table view with NSButtonCell (check box).
I was able to show checkboxes state (checked or unchecked), using following table column bindings:
Value - ArrayController.arrangedObjects.state
In this case it shows an array of checkboxes with title - "Check" as shown in below screen-shot:
Now my aim is to show checkboxes title using bindings, such that it
gets value from same mutable dictionary from which it is getting its
state.
I tried following binding for button cell but it did not work:
title -> ArrayController.selection.title
I also tried this binding for button cell :
title -> ArrayController.arrangedObjects.title
but it didn't work, it appeared like this after using above binding:
Can any one suggest me which controller key to use and if this is not the correct way to show titles then what is the correct way to do so?

Unfortunately you'll need to write a little code if you want to do it this way. When binding table column values to an array, the table column is handling taking the prototype data cell, setting its values, and "stamping" it in place for each row. The button cell's bindings aren't exposed "through" the table column, so a simple binding won't do it for you.
To Answer Your Question
So. Since only the value binding is exposed, the title must be set manually if you really want the checkbox's title to reflect the value (ie, you really want the checkbox to handle both the check state and displaying the title). To do this, you have to mix bindings with < NSTableDelegateProtocol > . Use the -tableView:willDisplayCell:forTableColumn:row: method to set the cell's -title property to that of the proper object in your array controller's -arrangedObjects array each time you're asked. Mixing bindings and data source / delegate methods is actually quite common for more than the most basic applications, so don't worry that you're doing something dirty. Note: you won't be able to support editing the title by doing this since it's a checkbox.
An Alternative Design
Personally, I'd avoid all that and just add a separate table column for the title. Bind the new column's value to the array controller's arrangedObjects.title and turn off the checkbox button cell's title so only the checkbox itself is displayed. That simplifies the whole thing greatly and allows editing the title.

Related

Pop Up Button Cell return a boolean using Cocoa Binding

I'm working on an NSTableView populated by an NSArrayController connected to a Core Data Entity with a few boolean attributes. I'm using Cocoa Bindings.
Most cells/columns are using the NSButtonCell class, which works great for those boolean attributes. They can be populated by the array and the user can modify them without issues.
However, for one of the columns, I want to give users the option to choose between two items (Item A, Item B) using a NSPopUpButtonCell instead of a checkbox. The value associated with that column is a boolean, so I'm currently getting an error
this class is not key value coding-compliant for the key value..
What would be the recommended way to configure that cell so that if the user selects Item A we pass 0, Item B we pass 1; and also populate the cell correctly (Item A or B) using the existing boolean value when the table is loaded?
You should use selectedIndex instead of value for NSPopUpButtonCell. See https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSPopUpButtonCell.html for complete list of NSPopUpButtonCell bindings.

Where can I bind column cell view controls?

I've been Googling for 3 hours, I can't find where to bind these things for Core Data in this textbook I'm reading. I just want to know where to find out where to bind it. I assume it's in the binding inspector after you highlight a column but under value it doesn't show anything like "Col 0 Image View" and for the first column I can't see a section that includes two places to set a binding which needs for an Image View and a Text Field. Below is an excerpt (as well as I can copy it) from the textbook Cocoa Programming for Mac OS X 4th Edition chapter 11, page 185.
"Now bind the value of each column's cell view control(s), as shown in the following table."
One of the entries in the table is:
Binding valueof Col 0 Image View
Bind to Table Cell View
Controller Key Empty
Key Path objectValue.photo
So after a day's work of searching and random clicking I found this rather elusive, yet out-in-the open binding. I hope this helps anyone else who might be frustrated.
This is where to find it, you want to click the one that shows what I have circled. Open the bindings inspector and then it'll pop up with objectValue already populated in your key path.
The first column has two since it has an image view and text view. Make sure to select the appropriate one for whichever one you want bound.

Binding to NSMatrix for BOOL attributes

I have a simple core-data entity that has a Boolean attribute called subscribedToNewsletter. I have designed my UI to represent this field as a radio button group with Yes/No as the text values (and obviously YES/NO as the backing BOOL values).
I have always considered myself to have excellent google-fu, but for the life of me, I am unable to find any clear documentation on:
how to bind the selected value of the NSMatrix (or is it the cell?) back to a YES/NO value
how to bind the textual values (if indeed I do need to; I know there are only ever going to be two values - surely they can be hard-coded in Interface Builder, or not?)
Next up will be how to bind a radio group (with Male/Female text) to a String field called gender that has M and F as possible values - but I digress.
Any pointers would be very much appreciated. Thanks a lot.
Not sure if I got no answers because a) it is such a stupid question, or b) no-one in the world knows how. The latter doesn't strike me as likely, so I can only presume the first :)
Anyway just in case there others as stupid as me, I thought I'd give some feedback on how I managed to achieve it.
In Interface Builder, I created a NSMatrix with two radio button cells with "Yes" and "No" as the text for each respectively. In the Attributes Inspector (command-1), make sure the Tag field is set to 1 for the "Yes" button cell, and set to 0 for the "No" button cell. Then, in the Bindings Inspector (command-4) bind the SelectedTag field for the matrix to the Boolean property on your entity. Easy as pie.
I make no claims that this is the best way... just that it is effective.
I have found one solution for this part-
how to bind the selected value of the
NSMatrix (or is it the cell?) back to
a YES/NO value
Here is the link to the problem which I faced and its solution- NSMatrix simple binding question
Hope this helps !

How can I force entry to complete in a NSTextField from a NSButton

I just noticed a problem when my user-interface is in a certain state. I have a table view with two columns both of which the user can enter data that is to be used later. There's also a button which acts upon the contents of the table view.
The problem is if the user has entered new data in the column but has not yet exited the field by using the tab key or return key (i.e. the cursor is still in the field and in editing mode) and the button is pressed the old value is used not the current value sitting in the field.
What is the best way to handle this this? I want to use whatever the user has entered thus far.
Basically, The button code needs to tell the text field to finish completion or exit the editing mode. But I can't seem to find a method that will do that.
Use bindings. In Interface Builder, select the table column and in the Inspector go to Table Column Bindings and set the Value content binding appropriately and ensure the "Continuously Updates Values" option is checked. Then changes to the table cell content will propagate immediately.
Found the answer, At least for me.
Find out if a row is selected and if so deselect it. This causes the current entry to be completed.
- (void) completeTableEntry
{
// If a column is selected ensure it is completed
NSInteger sr = [keyValueTable selectedRow];
if (sr != -1) {
[keyValueTable deselectRow:sr];
}
}
How about:
[theTargetWindowWhateverThatIs endEditingFor:nil];
theTargetWindowWhateverThatIs may be, for example, self.window if you are inside a NSWindowController.

Why do all the Checkbox's in my Outline View check when only one is clicked?

I am using a Outline view with two columns one a text field the other a check box field. Each column is binded to a NSTreeController. The problem I have is that when I click on a Checkbox I only want it to check that one check box but it checks all of them in the column. To help answer my Question this is the Bindings : http://snapplr.com/x5yh and this is the entity model: http://snapplr.com/vqc9
The controller key is wrong. Use arrangedObjects instead of selection.
selection will display the value of the current selected object.