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

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.

Related

NSOutlineView with manual Cocoa bindings

I have an NSOutlineView that is bound to a NSTreeController. The details are like this:
the treeController has class mode, and CommonListData as the Class Name, which has a "children" property
the treeController also has "content array" bound to File's Owner's "headersArray" (of type NSArray). The "headersArray" is an array of CommonListData items
the NSOutlineView has bindings for "Content" to the treeController's arrangedObjects
the view-based outlineView is designed in Interface Builder, with each cell view has lots of elements. Each element (labels, images etc) have 'value' bindings to the NSTableCellView with the relevant objectValue.xxx model key paths
The setup is fairly simple, and it all works fine in adding and deleting objects. But the problem is that it doesn't use any of the NSOutlineView / NSTableView animations when adding or removing items from the outline view. The table just reloads itself if I add or remove any element to the treeController, or directly to the headersArray's children objects. I'm not sure if that is a limitation with Cocoa Bindings or what.
In any case, I am consider whether I can disable all the bindings and do this manually. The only drawback is that the NSOutlineView cell views have complicated elements on them, and I don't want to have to map the view elements to the data in code. Ideally it should keep the bindings in the NSTableViewCell.
So my question is:
Is it possible to keep the bindings within Interface Builder in a NSTableCellView, but not bind the content to the treeController? Or is there an alternative to binding "arrangedObjects", such that adding and removing data from the NSTreeController doesn't trigger an update to the UI?
It would help to have some more control over the bindings, so that I can trigger the animations correctly, and not have it just jump around with every update.
I believe the bindings of the views within the cell view are independent of the bindings of the outline view's content. Whether the outline view uses bindings or not, it eventually sets the objectValue property of the cell view. So long as that's done in a KVO-compliant manner (which it is), any bindings to that property will work.

NSTableView & CoreData: Delete Object at clicked row

I am pretty new to Core Data and am currently working on a small (OSX) app that uses an NSTableView to organise objects. I would now like to delete a row/object with the click of a button on that targeted row.
I access the managed object within the table controller by calling [NSApp managedObjectContext] (still trying to figure out that dependency injection thing) but I can't easily delete an objectAtIndex: like I used to with the array (which has now been replaced by the core data stack, right?).
How do I identify the object to be deleted? And consequently, how can I cleanly remove it from the stack?
This is probably a really basic question but I couldn't find any resources on it. Bindings obviously don't work because the row does not get selected before the click occurs.
Any help is much appreciated!
Bindings would work, in that you could have the button's IBAction query the objectValue for the parent NSTableCellView. Once you have that objectValue, you could call the bound arrayController to delete the object, and then the cell/row would disappear.
So, if you have a tableCellView that has a delete button with an IBAction, within that IBAction, you could get the sender's superview, ensure it's an NSTableCellView, get the objectValue, and call [myArrayController removeObject:...]
As it says in the NSTableCellView class reference:
The objectValue is automatically set by the table when using bindings or is the object returned by the NSTableViewDataSource protocol method tableView:objectValueForTableColumn:row:.
This is actually a typical pattern with views in cocoa. objectValue or often representedObject are properties on the views that refer to the data model objects they represent, so if you have a view pointer from sender on the IBAction, you can get the related data model object. And if you're using bindings and a controller, you can then just have the controller remove that object.
With bindings, you will often create buttons that need IBActions attached, rather than some direct binding. But those IBActions can most definitely interact with the controller and not the view.
And with core data, array controllers are really slick vs. assuming you have to do it all programmatically.

NSTableView makeViewWithIdentifier across nibs

I have a similar question to Cocoa - View-Based NSTableView, using one cell in multiple tables, amplified by
Apple's own docs for makeViewWithIdentifier:owner:
"Typically identifier is associated with an external NIB in Interface Builder and the table view will automatically instantiate the NIB with the provided owner."
This seems to imply that you should be able to store the NSTableCellView in a separate nib from the nib containing the NSTableView. However, in my experimenting, I have only ever been able to obtain cells which are contained within the tableview I'm calling this on. I.e., if I cut and paste my cell into a new .xib file, the tableview can no longer find it. What am I doing wrong, or is this actually impossible and I am somehow misreading Apple's docs?
Use - (void)registerNib:(NSNib *)nib forIdentifier:(NSString *)identifier to register a nib to be used with a cell identifier.
If it doesn't work you're probably registering the nib after the tableView data has been loaded. Use [tableView reloadData] afterwords to be sure it's not a timing issue.
I just ran into this problem and I think you cannot use makeViewWithIdentifier:owner: when you're using a dedicated Nib to populate View-Based Tables.
The problem has to do with file owners (ie. view controllers). makeViewWithIdentifier:owner: seems intended to be used with "self" as the owner for simple custom views.
Generally if you have a separate nib for the custom view with outlets, you're going to want a separate view controller too. Otherwise, if your custom view has an outlet and the table displays many custom views, which outlet are you referring to from the "self" table view owner?
So in my test, I've got the AppDelegate as the delegate/datasource of the Table View. I have a CellView.xib, and CellViewController.h/.m with outlets to the interface. Then in my tableView:viewForTableColumn:row: delegate method I have this code:
SSCellViewController *vc = [[SSCellViewController alloc] initWithNibName:#"CellView" bundle:nil];
return vc.view;
What you lose is the cell re-use that happens automatically with makeViewWithIdentifier:owner:. To implement that yourself, you'll also likely have to deal with managing the many view controllers you've created.
I might still be missing something, as I'm coming to OS X development after years of only doing iOS work.

Global UITableViewCell identifiers, created once in storyboard and used in all tables of the app

I have an application that loads a cell in various table views. I want this specific cell to be the same across all tables view and every time i change something in its design to be populated in all views.
So far I have the cell creation method inside an object, which is call by all table views. If i make changes to that methods all table view are updated.
I have designed the cell in Storyboard and assign it a unique identifier. I want to keep it that way (not design it programmatically). So far I have to copy/paste the cell to all the tables that it is being used. Is there a solution to create a cell in storyboard that can be used in all table views without having to copy/paste.
Unfortunately we can't create a specifically table view cell in story board.
If you want to maintain a single cell instance for all the tables in the application, you need to to create a singleton instance and need to implement all the delegate and data source methods in that class. While setting the delgate and datesource objects to the table view object, you need to give the singleton instance. So that the implementation of the table view cells is single time through out the application and you don't need to copy paste all these methods for each table.
You can lay out the cell in its own xib file instead, create a UINib object from the nib, and associate that with the reuse identifier for your particular table.
Your nib must have a UITableViewCell (or subclass) as its top level object.
In viewDidLoad, you'd have something like:
UINib *sharedCell = [UINib nibWithName:#"SharedCell" bundle:nil];
[self.tableView registerNib:cellNib forReuseIdentifier:#"SharedCell"];
This way you can use the same design in several storyboards.

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