Note that there are some explanatory texts on larger screens.

plurals
  1. POSelection Highlight in NSCollectionView
    primarykey
    data
    text
    <p>I have a working <code>NSCollectionView</code> with one minor, but critical, exception. Getting and highlighting the selected item within the collection.</p> <p>I've had all this working prior to Snow Leopard, but something appears to have changed and I can't quite place my finger on it, so I took my <code>NSCollectionView</code> right back to a basic test and followed Apple's documentation for creating an NSCollectionView here:</p> <p><a href="http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/CollectionViews/Introduction/Introduction.html" rel="noreferrer">http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/CollectionViews/Introduction/Introduction.html</a></p> <p>The collection view works fine following the quick start guide. However, this guide doesn't discuss selection other than <code>"There are such features as incorporating image views, setting objects as selectable or not selectable and changing colors if they are selected"</code>. </p> <p>Using this as an example I went to the next step of binding the Array Controller to the <code>NSCollectionView</code> with the controller key <code>selectionIndexes</code>, thinking that this would bind any selection I make between the <code>NSCollectionView</code> and the array controller and thus firing off a KVO notification. I also set the <code>NSCollectionView</code> to be selectable in IB.</p> <p>There appears to be no selection delegate for <code>NSCollectionView</code> and unlike most Cocoa UI views, there appears to be no default selected highlight.</p> <p>So my problem really comes down to a related issue, but two distinct questions.</p> <ol> <li>How do I capture a selection of an item?</li> <li>How do I show a highlight of an item?</li> </ol> <p><code>NSCollectionView</code>'s programming guides seem to be few and far between and most searches via Google appear to pull up pre-Snow Leopard implementations, or use the view in a separate XIB file.</p> <p>For the latter (separate XIB file for the view), I don't see why this should be a pre-requisite otherwise I would have suspected that Apple would not have included the view in the same bundle as the collection view item.</p> <p>I know this is going to be a "can't see the wood for the trees" issue - so I'm prepared for the "doh!" moment.</p> <p>As usual, any and all help much appreciated.</p> <p><strong>Update 1</strong></p> <p>OK, so I figured finding the selected item(s), but have yet to figure the highlighting. For the interested on figuring the selected items (assuming you are following the Apple guide):</p> <p>In the controller (in my test case the App Delegate) I added the following:</p> <p><em>In awakeFromNib</em></p> <pre><code>[personArrayController addObserver:self forKeyPath:@"selectionIndexes" options:NSKeyValueObservingOptionNew context:nil]; </code></pre> <p><em>New Method</em></p> <pre><code>-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualTo:@"selectionIndexes"]) { if([[personArrayController selectedObjects] count] &gt; 0) { if ([[personArrayController selectedObjects] count] == 1) { personModel * pm = (PersonModel *) [[personArrayController selectedObjects] objectAtIndex:0]; NSLog(@"Only 1 selected: %@", [pm name]); } else { // More than one selected - iterate if need be } } } </code></pre> <p><em>Don't forget to dealloc for non-GC</em></p> <pre><code>-(void)dealloc { [personArrayController removeObserver:self forKeyPath:@"selectionIndexes"]; [super dealloc]; } </code></pre> <p>Still searching for the highlight resolution...</p> <p><strong>Update 2</strong></p> <p>Took Macatomy's advice but still had an issue. Posting the relevant class methods to see where I've gone wrong.</p> <p><em>MyView.h</em></p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface MyView : NSView { BOOL selected; } @property (readwrite) BOOL selected; @end </code></pre> <p><em>MyView.m</em></p> <pre><code>#import "MyView.h" @implementation MyView @synthesize selected; -(id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } -(void)drawRect:(NSRect)dirtyRect { NSRect outerFrame = NSMakeRect(0, 0, 143, 104); NSRect selectedFrame = NSInsetRect(outerFrame, 2, 2); if (selected) [[NSColor yellowColor] set]; else [[NSColor redColor] set]; [NSBezierPath strokeRect:selectedFrame]; } @end </code></pre> <p><em>MyCollectionViewItem.h</em></p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @class MyView; @interface MyCollectionViewItem : NSCollectionViewItem { } @end </code></pre> <p>"MyCollectionViewItem.m*</p> <pre><code>#import "MyCollectionViewItem.h" #import "MyView.h" @implementation MyCollectionViewItem -(void)setSelected:(BOOL)flag { [(MyView *)[self view] setSelected:flag]; [(MyView *)[self view] setNeedsDisplay:YES]; } @end </code></pre>
    singulars
    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.
 

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