How to add text to NSTableView - cocoa

I made a Cocoa application that has an NSTableView, an NSTextField, and an NSButton. The user enters text into the text field and clicks the button. When the user does this, I want the text in the textfield to be placed in the NSTableView. I find that I can't even bind an IBOutlet to a cell in the NSTableView. What should be done?

The Model-View-Controller pattern, which is used extensively in Cocoa is your friend here.
What you need to do is to bind the NSTableView to an array (The model). Then configure the button so that a click tells the controller to add the content of the text field to the array and if the bindings are set up correctly the NSTableView (The View) will be updated.

What I think you need to do is make a class, AppController for instance which will be your data source and the delegate of the NSTableView. So you need the following.
Two IBOutlets (one for the NSTextField and one for the NSTableView)
An IBAction for the NSButton.
Make those connections in Interface Builder.
Remember to use the mandatory delegate methods (there are two of them) so you can add the data from your data source (usually a collection class..an array, dictionary...etc.

Related

NSOutlineView (or NSTableView) with custom NSTableCellView doesn't create connections to outlets

A .nib file contains NSOutlineView (1-column, view-based). Column has identifier "UserCell"; it also has one cell which has two NSTextField's and bunch of buttons; this cell also has identifier "UserCell". Class for this cell is set in IB to be a custom class derived from NSTableCellView class. I option-dragged both NSTextField's to the custom code (see picture attached) to create IBOutlets.
In delegate's method -outlineView:viewForTableColumn:item: I load cell using -makeViewWithIdentifier:owner: and it loads fine - I get my custom cell view object. However, both text fields are nil's.
How one should fix it? The same story with NSTableView.

Make NSTableView cells editable programmatically only

How to configure a view-based NSTableView to behave like so:
Rows are selectable
The user are unable to trigger edit mode by clicking a cell
Edit mode can be triggered by calling NSTableView-editColumn:row:withEvent:select: programmatically
The table view is dragged from the object library of Xcode interface builder, i.e., it uses an NSTableCellView (with an NSImageView and an NSTextField as its subviews) as the table view's cell view.
For view-based table views, -editColumn:row:withEvent:select: is relatively ineffective. It attempts to make the cell view the first responder for the window, but only certain views will accept first responder status. NSTableCellView does not, because it is not itself editable.
If you want to programmatically initiate editing in the text field within an NSTableCellView, you can do something like:
NSTableCellView* cellView = (NSTableCellView*)[tableView viewAtColumn:col row:row makeIfNecessary:YES];
if ([cellView.textField acceptsFirstResponder])
[cellView.window makeFirstResponder:cellView.textField];
To disable the user from starting editing through the UI, I think you will need to set the text field to not be editable. You would make it editable just before you initiate editing programmatically. For example, add a line cellView.textField.editable = YES; between the above two lines.
Then, you'll want to set it back to non-editable after editing ends. To do this, you can set the delegate of the text field to your controller object and implement -controlTextDidEndEditing:. Or, similarly, you can add an observer of the NSControlTextDidEndEditingNotification notification from the text field. Either way, when your code is called, you set the text field's editable property back to false. (If you don't otherwise have a reference to the text field in question, you can obtain it from the NSNotification's object property.)

How to enable/disable editing in a view based NSTableView using bindings?

I cannot figure a way to enable/disable editing in view based NSTableViews using cocoa bindings.
I mean, I can perfectly enable/disable editing in a separate NSTextField, by binding it's "editable" attribute to a file's owner property such as
#property BOOL canModify;
(bind to: file's owner)
(Model Key Path: self.canModify)
But if I do exactly the same with a NSTextField in a view based TableView the binding seems to be totally ignored.
Also to be noticed that I can populate the table via bindings/array controllers so it's quite strange that the stuff doesn't work only for the "editable" property.
Thank you in advance
No. There's no problem in Apple's implementation of File's owner bindings for tablecellviews. I simply overlooked an IB warning.
"... Objects inside view-based cells may only be connected to the tableview's delegate."
I did set the "delegate" outlet of the TableView to my WindowController (implementing the "delegate protocol) and everything is working fine, without any double double double passages over table Cell's objectValues.
The name of your property and the key path of your binding don't match, although maybe that's just a typo: canModify vs. canModyfy ("i" vs. "y").
The editable binding should work just fine. You should check your other bindings, such as the value binding, to see if it has the Conditionally Sets Editable binding option enabled. Annoyingly, this option is enabled by default.
Lastly, you haven't explained in which NIB you're defining your cell views. Are they defined in the same NIB as the table view itself, or are they in a separate NIB? That would affect which object is File's Owner.
Even when table cell views are defined inside the same NIB as the table view, I believe they are encoded as a NIB-within-a-NIB. That is, each table cell view subhierarchy is actually encoded into a NIB blob and that NIB blob is archived into the parent NIB. When the table cell view sub-NIB is loaded, the table view's delegate is usually supplied as its owner. So, binding to File's Owner may not have the effect you expect.
For table cell views, you usually bind the subviews (like an NSTextField) to the table cell view itself, keyed off of the objectValue property.

Table cell only is highlighted when you tap on the disclosure icon

I place a single table view cell on my view using interface builder and add an accessory type, a selection color and enable user interaction.
My problem is that the cell only is highlighted when the user taps on the arrow icon. But it also should be highlighted when the user taps on the text label inside it, he can tap anywhere on the cell.
How can I achieve this?
A UITableViewCell is really only meant to be used within the context of a UITableView (which knows how to handle and/or delegate the selection & highlighting of the cell).
Having a UITableViewCell outside of the context of a table is a somewhat unexpected user interface.
Couldn't you do what you want to do with a UIButton instead?
Or a UIView that has two UIButton subviews (or at least one UIButton subview to contain a widget graphic that looks like the disclosure accessory)? That way the things you want to get highlighted would definitely happen, as opposed to depending on a nonexistent [UITableView didSelectCellAtIndexPath:] method that doesn't exist in a view that isn't a UITableView delegate.

How to get NSTableCellView of view-based NSTableView?

I've just created my first view-based NSTableView in Interface Builder and I've correctly set up the data source and the bindings to update the views in the tableview. Each view has two labels and a NSProgressIndicator. Updating the progress indicator through the bindings and the data source works perfectly, but I'd like to change its state from determinate to indeterminate at some time. As my NSTableCellView subclass has access to the progress indicator, how can I get access to the cell view at a given row index? I've tried calling viewAtColumn:row:makeIfNecessary: on the tableview with both NO and YES for the makeIfNecessary argument, but neither seems to work.
Solution 1: In your NSTableCellView subclass add a property (IBOutlet) for your NSProgressIndicator control. Wire it in IB to set the property when the view is loaded. You can then access the progress control in your cell view subclass by using the property.
Solution 2: In IB give your NSProgressIndicator a unique integer tag. In your cell view subclass use [self viewWithTag:] to get the object.
I am not sure about the answer to your main question but you can bind the indeterminate state as well. In IB Is Indeterminate is listed in the Parameters section.

Resources