我有一个NSCollectionView,指定为我的DataSource和代表。
我有两个问题:
registerClass方法,而是尝试使用(非零) protoNib的3行注释代码来向NSCollectionView注册,这导致theItem总是为0.。
使用类注册表选项的
willDisplayItem和didEndDisplayingItem存根,系统在第一次调用itemForRepresentedObjectAtIndexPath时就会占用内存(对这两个存根有数千次内部调用),并最终崩溃。仪器显示AppKit.正在创建数千个4k @autoreleasepool content items。
知道为什么会发生这种事吗?
-(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
{
}发布于 2020-12-26 09:56:52
Appkit类不是设计成它们自己的委托的。NSCollectionView实现了几个NSCollectionViewDelegate方法并调用了委托。我不知道为什么会这样实现,感觉也不对,但事实就是如此。如果集合视图是它自己的委托,并且没有在子类中实现委托方法,那么调用将导致无限循环。解决方案:不要将delegate设置为self。
https://stackoverflow.com/questions/65445630
复制相似问题