Binding NSTableView and NSArrayController together - objective-c

I have an app controlled by a big controller. In my principal window, I have a button, which when pressed I want to open a new window where data would be stored into an NSTableView.
After reading some documentation, I found something very interesting and useful between NSTableView and NSArrayController.
So I would like to know if it's possible to display data in an NSTableView controlled by a NSArrayController from an NSMutableArray edited in my Controller.

Yes, this is what NSArrayController is for. See Creating a Master-Detail Interface in Cocoa Bindings Programming Topics for full instructions on how to wire this up. You'll probably also want to look at Providing Controller Content if you're not already familiar with that part.

Related

Working with multiple NSTableView into NSWindowController?

I am working to a window with multiples NSTableView (in my case 8) and I am not sure if it's correct what I do.
My screen look like printscreen : custom window
In principle my window it's a Master - Detail Interface a selection of table from the left must populate the right table.
Until now I succeded to populate all the tables from the left with help of cocoa binding - NSArrayController which contain custom objects but further I don't know how continue, obvious in my mind it's to implement delegate method for NSTableView: tableViewSelectionDidChange:notification but this require more glue code.
My question if it's possible to populate the right table with help of cocoa binding without glue code. I read documention and I tried with selection proprety of NSArrayController but all my attempts failed. Any suggestion?

NSTableView problems - displaying custom TableView from with a SplitView panel

I am developing my first app for OSX. Sorry for asking stupid questions. I have spent a few hours trying to figure this out on my own, with no luck so far.
I want to make an iTunes-like interface. I used NSSplitView, placed NSView for navigation and NSTableView above that. [I am aware that there better alternatives to NSSplitView, yet my goal is to both - develop an app and also to learn Cocoa/OSX in the process.]
Atop NSView panel designated for navigation, I am trying to place NSTableView. However, my table is not being displayed. I therefore have questions...
I understand that for cells to be populated, controller must implement NSTableViewDataSource. I tried that, but was so far unsuccessful - to the point that I don't see the table. Please advise:
Can I have a working NSTableView-derived custom class also implementing NSTableViewDataSource? If this cannot work, please advise why or point me to an explanation.
Am I correct in thinking that all elements can be manipulated programmatically, in the sense that I use IBOutlet in headers to point to the right object, yet do nothing with InterfaceBuilder - have everything controlled from within my Objective-C code? Do I have to use IB?
Thank you.
Yes that will work but it's an unusual approach. Generally the tableview delegate/datasource is something enclosing the tableview. You'd normally only subclass NSTableView if you require some additional functionality not provided by default (for me that has been custom behaviour to input).
Yes you can do it all programmatically, however you will find it much easier to use IB. The IB-loaded views are created programmatically under the hood, using the information contained in the nib file. You will find it long-winded and tedious pretty quickly.
WRT to your issue with not seeing the table, you will need to add logging/breakpoints on the few key delegate/datasource methods to ensure they are being called (start with the daddy of them all numberOfRowsInTableView:). If they are not then you aren't setting the delegate/datasource correctly in the tableview.

Clicking on an image in a collection view

I have a collection view and each item has an image and a label. I want to click the NSCollectionViewItem or NSImage and then hide the collection view and display a completely separate view containing the details of the object that was clicked.
I cant find any documentation on how to handle click events in this situation. How is this possible? I have built out the collection view in Interface Builder so everything was done via bindings as opposed to code.
#Jeff, I don't have permissions to add a comment so writing this as answer.
You can overwrite setSelection in your subclass of NSCollectionViewItem (as explained by #indragie in Selection Highlight in NSCollectionView) to track the selected item and perform an action.
The solution that I went with was to not actually use an Image Well, aka NSImage. I used a button and bound the Image property to an instance of an NSImage that I exposed as a property on my model.
It was easy enough but I'm shocked more people havent been asking this question.

Mutliple views for detail in a master-detail interface

I am building a preferences pane for an app where the user can add a Web Service to a list (in a tableview on the left) and a form should appear on the right where the user can edit configuration options for the web service (like username or password or API key or tumblr blog name, etc.) I think I'll need different views for different types of service (possibly with different logic for validation and so on).
My question is what is the best way to implement this?
What I've done: I have a NSSplitView with a NSTableView. I have an NSArrayController with it's content bound to the appropriate key in NSUserDefaultsController and the NSTableView bound to it's arrangedObjects and selectionIndexes. Next I've added a NSTabView in the right with it's selectedIndex bound to the NSArrayController's selectedIndex and I'm trying to programatically insert the appropriate views (which I've created as separate custom views in IB) as tabs.
This seems to me to be a not the best approach. I also don't know what to bind the fields in the detail views - is it even possible to use bindings here? How would you solve this problem?
Instead of the NSTabView, create a blank NSView for the inspector (inspectorView). Same idea, but a little simpler.
In tableViewSelectionDidChange, write something like:
newView = ...;
if (inspectorView != [customInspectorView superview]) {
NSView *oldView = [[inspectorView subviews] objectAtIndex:0];
[inspectorView replaceSubview:oldView with:newView];
}
You can bind your fields to servicesArrayController.selection.username and so on.

NSTableView. How to override autoscroll behavior?

I've got an NSTableView that displays (via bindings) data from an NSTreeController. The application frequently appends/changes data to/in the bound array.
The problem is that if the user has selected a row in the table, but has scrolled so that the selected data is no longer visible, when the application updates the array it causes the display to auto-scroll so that the selected line is once again on screen. This is pretty
frustrating to users, especially since new data can arrive at any time.
Is there any way of disabling this feature?
You may have to subclass NSTableView and override -scrollRowToVisible:, temporarily bracketing the call to super. This may also require a custom BOOL ivar in your subclass to keep track of whether you want to scroll.
I would start by setting a breakpoint there to see when exactly the autoscroll is triggered. This should help to find the proper moments to toggle the ivar.
Are you using an NSTreeController with an NSOutlineView or an NSArrayController with an NSTableView? Using an NSTreeController with an NSTableView doesn't make a lot of sense to me?
If you're using an NSTableView you should probably be using an NSArrayController to manage its data and this rearranging of the rows is a feature of the NSArrayController. Try turning off the Auto Rearrange Content option on your controller within IB.
When it's on, the array controller will rearrange its objects on addition, removal and changes to objects that would affect the sort ordering (if any) and cause any table views or outline views to reload their data.
I don't know of a similar feature for NSTreeController mainly because I don't use it since it's never worked well for me. I, sadly, recommend to just use the datasource methods for the NSOutlineView and supply your data the old-fashioned way. In my experience, NSTreeController is only well suited for the most trivial tasks.