Pregunta

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:

enter image description here

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:

enter image description here

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top