Populate NSForm with NSComboBoxes - objective-c

So I have a task of reading from an xml file which contains a description of what form fields and comboboxes should exist in a dialog. I started by using NSForm and addentry to add the form fields, but then found out NSMatrix may be required to add combobox cells dynamically.
So my questions are:
1) Since NSForm inherits from NSMatrix can I add combobox cells to NSForm after I add the text fields.
2) If I have to use NSMatrix, does anyone have any good sample code they can point me to or write which adds a text field, and combobox to it at runtime and then resizes the NSMatrix to fit its contents. A lot of books just describe what NSMatrix is, and show how to populare it using interface builder.

NSForm does not allow custom cell types (text field only) and NSMatrix takes only one cell type (you cannot have an NSMatrix that has a mix of NSTextFieldCell and NSComboBoxCell).
If you really need to create a variable number of rows of field + combo box, you're going to have to manage the creation, layout, and destruction of these manually.
Alternatively, you might consider using collection views.
Update - If you're targeting 10.7 and up and need better control than collection views offer, consider using view-based table views.

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.

NSTableView group row text field selection

I want to make a custom text selection in my table view.
I found this awesome answer on how to set the colors, fonts, etc. of the cells.
However, there is one issue.
It does not work for group rows. I see that the method gets invoked, but I think the style gets overwritten somewhere in either the NSTableCellView, or in the NSTableView class.
How can I fix this?
EDIT
It's a view based NSTableView

Is there a way to display a different small image (icon) for each row of a NSTableColumn?

Is there a way to display a different small image (icon) for each row of a NSTableColumn ?
I don't necessarily need to add a new column for it, I was wondering if I can just add the icon in front of the text of each row.
I know there is the method: - (void)setDataCell:(NSCell *)aCell . However this method seems to use the same cell for all rows, which is not what I want.
Is there a solution to this problem which doesn't require to subclass the NSTableColumn ? If not, what should I subclass ?
thanks
Make the cell for the column in question an NSImageCell. You can do this in Interface Builder by dragging an "Image Cell" from the object library onto the column. Then select the column and you'll be able to bind to an image source via one of the Data, Value, Value Path, or Valueurl bindings. Which one you choose depends on whether the source property in your model returns NSData, NSImage, NSString path, or NSURL url, respectively. See the documentation on NSImageCell bindings for more complete information.
Cells are the same for each row, because they're a lightweight class used as a performance optimization for drawing. That doesn't mean that each row has to use the cell to draw the same image though, any more than the text columns' cells have to draw the same string for each row.
If you really need the image to be in the same column as text, you'll have to do something more like duDE explains. Off the top of my head, I believe bindings should be still possible in this case, but you'll have to write more code to make that work. Rather than do that, I would probably switch to using a view based NSTableView, which makes putting multiple views in a single column/row easy, and will let you continue to use bindings without any extra work.
EDIT: duDE's answer was deleted, but to summarize, it suggested creating a custom NSCell subclass that would draw both an image and text.

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.

How to disable sorting in NSTableVIew?

I have a an NSTableView when ever I click on a specific header column the data in the table get reversed or sort upside down. I have checked NSTableView as well as NSTableColumn but couldn't find any method that disables this. I would be obliged if anyone can help in disabling this sorting on clicking on the header of a particular column.
Sorting of the NSTableView is done by its sortDescriptors, see here.
An NSTableColumn uses its sortDescriptorPrototype (see here) to generate the sort descriptor of the NSTableView, depending on how many times you clicked the column header, etc.
If you use dataSource to manage the data, then the sort descriptor is communicated via the delegate method tableView:sortDescriptorsDidChange:, see here. You just need to ignore the change message to stop sorting.
If you use Cocoa bindings to manage the data, the sort descriptor is generated by the table column and set to the NSArrayController. To stop it, just open the inspector of the binding of the table column, select value, and uncheck "Creates Sort Descriptor."