I've been struggling with trying to create an NSCollectionView that has a set of NSCollectionViewItems with a custom view. The code works fine when the controls on the item view are standard AppKit controls, but once I add a custom NSView, there's no way to bind it from Interface Builder.
From spending some hours searching the internet, there appears to be a lot of options to solve this but all seem specialised. Is there some simple example code that demonstrates how, given a CustomImage * on the item view, to set the image property on that custom view?
The model that provides data for each item is:
@interface MyItem : NSObject
@property (retain, readwrite) NSImage * image;
@property (retain, readwrite) NSString * name;
@end
The NSCollectionViewItem subclass is:
@interface MyCollectionViewItem : NSCollectionViewItem
// Properties
@property (strong) IBOutlet NSTextField * name;
@property (strong) IBOutlet CustomImage * image;
@end
where CustomImage is simply a subclass of NSImageView.
I tried subclassing NSCollectionView and overriding newItemForRepresentedObject as some answers suggested and assigning there:
MyItem * item = (MyItem *)object;
MyCollectionViewItem * newItem = (MyCollectionViewItem *)[super newItemForRepresentedObject:object];
NSView *view = [newItem view];
[view bind:@"name" toObject:item withKeyPath:@"name" options:nil];
[view bind:@"image" toObject:item withKeyPath:@"image" options:nil];
return newItem;
but this just blew up in the bind call with an error that 'name' doesn't exist.
This should, in theory, be an extremely simple thing to solve but none of the answers I've found make this clear. An alternative would be to ditch NSCollectionView and use one of the simpler replacements on GitHub but I'd like to have a last attempt to see if this is solvable first.
Thanks!
name
blowing up. – Daniel FarrellCustomImage
a view class or a model class? Neither ofMyItem
orMyCollectionViewItem
is a view, but there's an attempt to bindname
andimage
bindings of theview
object, which would not have support for such bindings by default. Also, oneimage
property is anNSImage
while the other is aCustomImage
but it's not clear how that's expected to be mapped. You need to show at least the interface forCustomImage
. – Ken Thomases