Nscollectionviewitem Never Instantiate

Programmatically created NSCollectionView `makeItem` returns nil even if registered

I think this is a bug. Workaround: set collectionViewLayout before registering the class.

Apparently the registered class is not stored if collectionViewLayout is not set. collectionViewLayout can be set to a new layout after registering the class, the registered class isn't removed.

NSCollectionView doesn't seem to need registering its item class

Apple has a sample application called CocoaSlideCollection that demonstrates the use of the modern (i.e. 10.11+) NSCollectionView. In the collectionView:itemForRepresentedObjectAtIndexPath: method in the file AAPLBrowserWindowController.m, there is this comment:

Message back to the collectionView, asking it to make a @"Slide" item associated with the given item indexPath. The collectionView will first check whether an NSNib or item Class has been registered with that name (via -registerNib:forItemWithIdentifier: or -registerClass:forItemWithIdentifier:). Failing that, the collectionView will search for a .nib file named "Slide". Since our .nib file is named "Slide.nib", no registration is necessary.

While I didn't see this explicitly mentioned in the documentation, this suggests that as long as the .xib name matches the identifier (and has one and only one NSCollectionViewItem or subclass of in the file), then registration isn't needed.

How to use NSCollectionViewItem without subclassing it?

Here is a very simple example which uses a Nib for the item's prototype:

@interface ViewController()

@property (weak) IBOutlet NSCollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSNib *theNib = [[NSNib alloc] initWithNibNamed:@"Item" bundle:nil];

[self.collectionView registerNib:theNib forItemWithIdentifier:@"item"];
}

#pragma mark NSCollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)inCollectionView {
return 1;
}

- (NSInteger)collectionView:(NSCollectionView *)inCollectionView numberOfItemsInSection:(NSInteger)inSection {
return 10;
}

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)inCollectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)inIndexPath {
NSCollectionViewItem *theItem = [inCollectionView makeItemWithIdentifier:@"item" forIndexPath:inIndexPath];
NSTextField *theLabel = (NSTextField *)theItem.view;

theLabel.stringValue = [NSString stringWithFormat:@"%d.%d", (int)inIndexPath.section, (int)inIndexPath.item];
return theItem;
}

@end

The NIB contains just a NSCollectionViewItem with a text field as view.

Addendum:
I think you should create a NSCollectionViewItem.xib for the registerClass variant. A view controller will search for a NIB with its class name, if you doesn't create its view manually in loadView. Thus, you can't use plain NSCollectionViewItem without a NIB for registering a class, because of makeItemWithIdentifier:forIndexPath: will access the view of the item.

How do you initialize a NSCollectionViewItem?

I found this documentation - NSCollectionViewItem class

What I found there shows setting a reference like so:

Setting the Represented Object

– representedObject Available in Mac OS X v10.5 through Mac OS X v10.5
– setRepresentedObject: Available in Mac OS X v10.5 through Mac OS X v10.5

Your sample:
-(void)setSelected:(BOOL)flag

I don't know the language but is BOOL an id?

setRepresentedObject:

Sets the receiver’s represented object
to the specified model object.
(Available in Mac OS X v10.5 through
Mac OS X v10.5.)
- (void)setRepresentedObject:(id)object
Parameters

object

The receiver’s model object.

Availability

Available in Mac OS X v10.5 through Mac OS X v10.5.

Declared In NSCollectionView.h

Note: I did see this on the documentation:

Important: In Mac OS X v10.5, the superclass of the NSCollectionViewItem class was NSObject. In Mac OS X v10.6 and later, NSCollectionViewItem is now a subclass of NSViewController. This change was made to improve how the view is replicated within the NSCollectionView. NSCollectionViewItem remains binary compatible with the previous implementation and unarchiving is correctly handled.

So, if you are used to working with an older API, there may have been some changes since you last did this ...???

NSCollectionview didSelectItemsAt never called

here is my solution: NSCollectionView item could not be selected on default,we must set

collectionview.isSelectable = true


Related Topics



Leave a reply



Submit