Question

How do I make the NSCollectionView update to show the currently selected item using an NSBox? Displaying selection in a list seems like a basic thing, but I'm having all kinds of trouble with this.

I've read this question and also looked at the sample code from Apple. There seems to be several ways to do this.

  1. Using a subclasses of NSCollectionViewItem and special "prototype view".
  2. Using a NSBox.

I wish to use the NSBox way since it seems simples and is also used in the official code sample.

It's apparently done as described in the following quote by alternegro:

If a different background color will suffice as a highlight, you could simply use an NSBox as the root item for you collection item view. Fill the NSBox with the highlight color of your choice. Set the NSBox to Custom so the fill will work. Set the NSBox to transparent.

Bind the transparency attribute of the NSBox to the selected attribute of File Owner(Collection Item) Set the value transformer for the transparent binding to NSNegateBoolean.

I'm stuck at the very first part: "use an NSBox as the root item for you (sic) collection item view". I've tried to change the "Custom Class" to a FoobarBox that inherits from NSBox, but it doesnt seems to help as I cannot change the background color to blue nor can I bind the transparency. Any pointers on how to make the selection display in my NSCollectionVuew would be appreciated.

Was it helpful?

Solution

In XCode 4.5.2, you can just delete the NSView that comes automatically with the NSColletionView and drag in an NSBox (which will have all the appropriate bindings available). Make sure you re-bind the CollectionView to your new Box.

OTHER TIPS

First, create a class for your ListView that inherits from NSBox

 @interface MyListViewBox : NSBox

    @property (unsafe_unretained) IBOutlet NSCollectionViewItem *controller;

 @end

Then, in Interface Builder, specify your class name as "Custom class" property as shown on my screenshot

screenshot

Then you will realize IB does not show NSBox properties or binding in the GUI (at least with version 4.5.2), so I decided to change the properties programmatically.

  • Create an outlet for NSCollectionViewItem in your NSBox subclass (as seen above)
  • Use IB to link the outlet to your NSCollectionItemView

  • in -(void)awakeFromNib for your NSBox subclass, add the following code

    -(void)awakeFromNib { 
    
      //properties are not showing up in XCode Inspector IB view
      //configuring the box here :-(
    
      self.boxType = NSBoxCustom;
      self.borderType = NSLineBorder;
      self.fillColor = [NSColor selectedControlColor];
    
      //bind the "transparent" property of NSBox to the "selected" property of NSCollectionViewItem controller
      //controller is bound as IBOutlet in IB
      NSValueTransformer* transformer = [NSValueTransformer valueTransformerForName:NSNegateBooleanTransformerName];
      [self bind:@"transparent"
        toObject:self.controller 
        withKeyPath:@"selected"
        options:[NSDictionary dictionaryWithObjectsAndKeys:transformer, NSValueTransformerBindingOption, nil]];
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top