How to get a presented object from a view in NSCollectionViewItem?

I have a view that will be used in each of my CollectionView elements. I have an IBOutlet for CollectionViewItem from my view and I connected to it in Interface Builder. I want to access a value from a createdObject (which is a Core Data object) in my view code. Here is an example of what I'm trying to do - access the sequence value of the represented object:

In the .h file:

IBOutlet NSCollectionViewItem *item; // Connected in IB 

In the .m file

 NSString *seq = [[item representedObject] valueForKey:@"seq"]; NSLog(@"Seq: %@", seq); // returns Seq: (null) 

I know that seq is populated because I linked it to a label in the CollectionViewItem in IB using the key path presentationObject.seq, and it works.

Any idea why, when I try to access the value for seq in the code for the view, it returns null?

+6
objective-c cocoa cocoa-bindings nscollectionview
source share
4 answers

It is very likely that NSCollectionViewItem does not copy IBOutlet connections from their element representations to the NSCollectionViewItem prototype. Therefore, item is nil, so seq will also be nil.

A typical example of accessing an NSCollectionViewItem instance is binding to a prototype. You mention that you did it and that it works. This is simply because it is a typical, supported way to do this.

If you really need to connect directly to the element in such a way that bindings cannot be provided, you may have to configure it manually. One way to do this is to override NSCollectionViewItem -copyWithZone: call super, and then make the connection manually.

+1
source share

1) An NSView subclass that is used to render an item in a collection view

2) In this subclass add the delegate property

3) Subgroup NSCollectionViewItem

4) Override the setView: method. Add a line to set the view delegate property to an instance of NSCollectionViewItem (i.e. self).

Now, in a subclass of NSView, you will access the corresponding NSCollectionView element through the delegate property (for example, you can access its represented object).

+1
source share

I had the same problem, but I found a very simple solution: I put the view in another .xib and bound it in NSCollectionViewItem (which is actually an NSViewController). Then .xib will be deserialized for each cell and all outputs are set.

0
source share
  • Subclass of NSView.
  • Add a delegate method to the specified subclass ... something like - (void)mouseDownOnItemAtIndex:(NSUInteger)index to tell the listener that the user clicked on an element at a specific index.
  • Extend the collection view using self.superview in the same class. Create a variable to store NSUInteger. Save the item index using the [[collectionView subviews] indexOfObject:self] method. Pass this index in the delegate method parameter of index .
  • A class that conforms to the protocol defined in your NSView will now be able to hear any mousedown events in a particular cell. If you want the represented object of a specific item, just call collectionView:itemAtIndex and get the item by accessing it through the representedObject property on the NSCollectionViewItem .
0
source share

All Articles