Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I’m not sure there’s much insight in creating a collection view programmatically and without bindings, but here it goes.</p> <h1>Introduction</h1> <p>There are essentially four components when using a collection view:</p> <ul> <li>View: a subclass of <code>NSView</code>, responsible for displaying information;</li> <li>The collection view itself;</li> <li>View controller: a subclass of <code>NSCollectionViewItem</code> that serves as the collection view item prototype;</li> <li>Model: an array of objects.</li> </ul> <p>Usually a view is designed in Interface Builder, and a model is mediated by Cocoa bindings.</p> <p>Doing it programmatically:</p> <h1>Constants</h1> <pre><code>static const NSSize buttonSize = {80, 20}; static const NSSize itemSize = {100, 40}; static const NSPoint buttonOrigin = {10, 10}; </code></pre> <h1>View</h1> <p>This is a standard view (a custom view in Interface Builder parlance) containing a button. Note that the view has fixed size.</p> <pre><code>@interface BVView : NSView @property (weak) NSButton *button; @end @implementation BVView @synthesize button; - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:(NSRect){frameRect.origin, itemSize}]; if (self) { NSButton *newButton = [[NSButton alloc] initWithFrame:(NSRect){buttonOrigin, buttonSize}]; [self addSubview:newButton]; self.button = newButton; } return self; } @end </code></pre> <h1>View Controller (Prototype)</h1> <p>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 <code>-setView:</code> before <code>-view</code> is received by the view controller, or override <code>-loadView</code>. The following code does the latter.</p> <p>View controllers receive the corresponding model object via <code>-setRepresentedObject:</code>. 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.</p> <p>Note that none of this code is specific to collection views — it’s general view controller behaviour.</p> <pre><code>@interface BVPrototype : NSCollectionViewItem @end @implementation BVPrototype - (void)loadView { [self setView:[[BVView alloc] initWithFrame:NSZeroRect]]; } - (void)setRepresentedObject:(id)representedObject { [super setRepresentedObject:representedObject]; [[(BVView *)[self view] button] setTitle:representedObject]; } @end </code></pre> <h1>Model</h1> <p>A simple array of strings representing button titles:</p> <pre><code>@property (strong) NSArray *titles; self.titles = [NSArray arrayWithObjects:@"Case", @"Molly", @"Armitage", @"Hideo", @"The Finn", @"Maelcum", @"Wintermute", @"Neuromancer", nil]; </code></pre> <h1>Collection View</h1> <p>So far, the only relation that’s been established is the view (<code>BVView</code>) used by the item prototype (<code>BVPrototype</code>). The collection view must be informed of the prototype it should be using as well as the model from which to obtain data.</p> <pre><code>NSCollectionView *cv = [[NSCollectionView alloc] initWithFrame:[[[self window] contentView] frame]]; [cv setItemPrototype:[BVPrototype new]]; [cv setContent:[self titles]]; </code></pre> <h1>Full Source Code for the Application Delegate</h1> <pre><code>#import "BVAppDelegate.h" static const NSSize buttonSize = { 80, 20 }; static const NSSize itemSize = { 100, 40 }; static const NSPoint buttonOrigin = { 10, 10 }; @interface BVView : NSView @property (weak) NSButton *button; @end @implementation BVView @synthesize button; - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:(NSRect){frameRect.origin, itemSize}]; if (self) { NSButton *newButton = [[NSButton alloc] initWithFrame:(NSRect){buttonOrigin, buttonSize}]; [self addSubview:newButton]; self.button = newButton; } return self; } @end @interface BVPrototype : NSCollectionViewItem @end @implementation BVPrototype - (void)loadView { [self setView:[[BVView alloc] initWithFrame:NSZeroRect]]; } - (void)setRepresentedObject:(id)representedObject { [super setRepresentedObject:representedObject]; [[(BVView *)[self view] button] setTitle:representedObject]; } @end @interface BVAppDelegate () @property (strong) NSArray *titles; @end @implementation BVAppDelegate @synthesize window = _window; @synthesize titles; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { self.titles = [NSArray arrayWithObjects:@"Case", @"Molly", @"Armitage", @"Hideo", @"The Finn", @"Maelcum", @"Wintermute", @"Neuromancer", nil]; NSCollectionView *cv = [[NSCollectionView alloc] initWithFrame:[[[self window] contentView] frame]]; [cv setItemPrototype:[BVPrototype new]]; [cv setContent:[self titles]]; [cv setAutoresizingMask:(NSViewMinXMargin | NSViewWidthSizable | NSViewMaxXMargin | NSViewMinYMargin | NSViewHeightSizable | NSViewMaxYMargin)]; [[[self window] contentView] addSubview:cv]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload