NSCollectionView
remains one of the most mysterious parts of the Cocoa API that I've ever seen. Documentation is poor and there are many moving parts, many of which are often implemented in Interface Builder, making documentation challenging.
Please provide sample code to create the simplest case of NSCollectionView
which displays either Text Fields or Buttons without using Xcode where each Text Field or Button has a different Title. Assume a new Xcode project with the default window
IBOutlet.
For this example, no binding is required to update the NSCollectionView as the data source changes. Simply display a grid of prototype objects and set each object's Title to some value.
If we can get a good example of how to do this available to many people, I think it will help everyone who works with NSCollectionViews
and is as baffled as I am.
Summary of request
- Provide sample code to render an NSCollectionView in a new Xcode project
- Do not use Interface Builder, do use the default window IBOutlet provided
- NSCollectionView should contain Text Fields or Buttons, your choice
- Each item in the view should have a different Title
- No binding is required
If there's sample code out there that meets these requirements, please provide a link, that'd be great!
To answer brigadir's question on how to bind to a mutable array.
zero'th - make titles an
NSMutableArray
first - bind the array to your items
Second - when altering titles, make sure to modify the proxy.
e.g.
I’m not sure there’s much insight in creating a collection view programmatically and without bindings, but here it goes.
Introduction
There are essentially four components when using a collection view:
NSView
, responsible for displaying information;NSCollectionViewItem
that serves as the collection view item prototype;Usually a view is designed in Interface Builder, and a model is mediated by Cocoa bindings.
Doing it programmatically:
Constants
View
This is a standard view (a custom view in Interface Builder parlance) containing a button. Note that the view has fixed size.
View Controller (Prototype)
Normally a view controller loads its view from a nib file. In the rare cases where the view controller doesn’t obtain its view from a nib file, the developer must either send it
-setView:
before-view
is received by the view controller, or override-loadView
. The following code does the latter.View controllers receive the corresponding model object via
-setRepresentedObject:
. I’ve overridden it so as to update the button title whenever the model object changes. Note that this can be accomplished by using Cocoa bindings without any code at all.Note that none of this code is specific to collection views — it’s general view controller behaviour.
Model
A simple array of strings representing button titles:
Collection View
So far, the only relation that’s been established is the view (
BVView
) used by the item prototype (BVPrototype
). The collection view must be informed of the prototype it should be using as well as the model from which to obtain data.Full Source Code for the Application Delegate
@Bavarious You did an excellent job there. This was just an amazing tutorial which I sometimes miss at the Apple Docs.
I rewrote Bavarious' code in Swift (v2) for anyone who's interested:
// AppDelegate.swift:
// BVTemplate.swift:
// BVView.swift: