I have an NSCollectionView specified as both my DataSource and my Delegate.
I have two issues:
Rather than doing the
registerClassmethod, attempting to instead use the 3 lines of commented code with the (non-nil) protoNib means of registering with anNSCollectionViewcausestheItemto always be nil.Using the class registry option, all works mostly fine. But if I remove the
willDisplayItemanddidEndDisplayingItemstubs, the system eats up gobs of memory on its first call toitemForRepresentedObjectAtIndexPath(with thousands of internal calls to these two stubs) and eventually crashes. Instruments shows thousands of 4k@autoreleasepool content itemsbeing created byAppKit.
Any idea why this might be happening?
-(void)awakeFromNib {
[self registerClass:[MECollectionViewItem class] forItemWithIdentifier:@"EntityItem"];
// NSString *nibName = NSStringFromClass([MECollectionViewItem class]);
// NSNib *protoNib = [[NSNib alloc] initWithNibNamed:nibName bundle:nil];
// [self registerNib:protoNib forItemWithIdentifier:@"EntityItem"];
__weak typeof(self) weakSelf = self;
[self setDelegate:weakSelf];
[self setDataSource:weakSelf];
...
}
- (MECollectionViewItem *)collectionView:(NSCollectionView *)collectionView
itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath;
{
MECollectionViewItem *theItem = [self makeItemWithIdentifier:@"EntityItem"
forIndexPath:indexPath];
return theItem;
}
-(void)collectionView:(NSCollectionView *)collectionView
willDisplayItem:(NSCollectionViewItem *)item
forRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
}
-(void)collectionView:(NSCollectionView *)collectionView
didEndDisplayingItem:(nonnull NSCollectionViewItem *)item
forRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath
{
}
The Appkit classes are not designed to be their own delegate.
NSCollectionViewimplements severalNSCollectionViewDelegatemethods and calls the delegate. I don't know why it's implemented like this and it doesn't feel right but it is what it is. If the collection view is its own delegate and a delegate method isn't implemented in the subclass then the call causes an infinite loop. Solution: don't setdelegatetoself.