How to show the button title (check box) in a table using bindings

I am trying a simple application where I have a mutable array of mutable dictionaries like

NSMutableDictionary *sample6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"title6",@"title",[NSNumber numberWithBool:NO],@"state", nil]; 

In IB, I created a table view with NSButtonCell (checkbox).

I managed to show the status of the checkboxes (checked or not checked) using the following table column bindings:

 Value - ArrayController.arrangedObjects.state 

In this case, it shows an array of flags with the heading β€œCheck”, as shown in the screenshot below:

enter image description here

Now my goal is to show the checkbox header using bindings so that it gets the value from the same mutable dictionary from which it gets its state.

I tried the following binding for the button cell, but this did not work:

title β†’ ArrayController.selection.title

I also tried this binding for the button cell:

title β†’ ArrayController.arrangedObjects.title

but this did not work, it appeared like this, after using the above:

enter image description here

Can someone suggest me which controller key to use, and if this is not the right way to show the headers, then what is the right way to do this?

+4
source share
1 answer

Unfortunately, you need to write a little code if you want to do it this way. When binding the values ​​of a table column to an array, the table column processes the prototype data element, sets its values ​​and "stamps" it in place for each row. Button cell binding does not appear β€œacross” the table column, so a simple snap does not do this for you.

Answer the question

So. Since only a binding of values ​​is displayed, the header must be set manually if you really want the flag header to reflect the value (i.e. you really want the flag to handle both the verification status and the header display). To do this, you need to mix the bindings with < NSTableDelegateProtocol > . Use the - tableView: willDisplayCell: forTableColumn: row: method to set the cell -title property to the corresponding object in the array controller -arrangedObjects array every time you are asked. Mixing bindings and data source / delegate methods is actually quite common for most mainstream applications, so don't worry that you are doing something dirty. Note: you cannot support editing the title by doing this, as this is a check box.

Alternative design

Personally, I avoid all of this and just add a separate table column for the header. Bind the new column value to the controller of the arrangedObjects.title array and turn off the cell button header of the flag button so that only the flag itself is displayed. This greatly simplifies all this and allows you to edit the title.

+6
source

Source: https://habr.com/ru/post/1380616/


All Articles