如何初始化 NSCollectionViewItem?
我正在尝试设置一个 NSCollectionView,它在各个 NSCollectionViewItem 视图中具有自定义绘图。我需要在每个视图中绘制一个图像,但无法将该视图链接回 Interface Builder 中的 NSCollectionViewItem 子类。是否有一个 init 方法可以与我的 NSCollectionViewItem 一起使用来执行初始化操作?我尝试实现 copyWithZone,但我做错了一些事情,因为我遇到了一些永恒循环。目前,我发现与视图建立连接的唯一机会是在使用 -(void)setSelected:(BOOL)flag 更改选择之后。我想在视图中进行绘图,但我需要来自representedObject 的图像作为我的源。我读到的所有与 NSCollectionView 相关的内容似乎都不完整。
@implementation CollectionViewItem
-(void)setSelected:(BOOL)flag {
[super setSelected:flag];
NSLog(@"setSelected: %d", flag);
// tell the view that it has been selected
[(CollectionViewItemView* )[self view] setSelected:flag];
// This is where I pass my image to my view
[(CollectionViewItemView* )[self view] setOriginalSprite:[(MyModel* )self.representedObject imageSource]];
[(CollectionViewItemView* )[self view] setNeedsDisplay:YES];
}
@end
I am trying to setup an NSCollectionView that has custom drawing in the individual NSCollectionViewItem views. I have an image that I need to draw in each view, but I cannot link the view back to the NSCollectionViewItem subclass in Interface Builder. Is there an init method I can use with my NSCollectionViewItem in order to perform initialization operations? I tried to implement copyWithZone, but I was doing something wrong because I got some eternal loop. Currently, the only opportunity I have found to make my connections to the view are after the selection has changed using -(void)setSelected:(BOOL)flag. I want to do my drawing in the view, but I need an image from my representedObject as my source. Everything I read that is NSCollectionView related, is seemingly incomplete.
@implementation CollectionViewItem
-(void)setSelected:(BOOL)flag {
[super setSelected:flag];
NSLog(@"setSelected: %d", flag);
// tell the view that it has been selected
[(CollectionViewItemView* )[self view] setSelected:flag];
// This is where I pass my image to my view
[(CollectionViewItemView* )[self view] setOriginalSprite:[(MyModel* )self.representedObject imageSource]];
[(CollectionViewItemView* )[self view] setNeedsDisplay:YES];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了这个文档 - NSCollectionViewItem 类
我在那里发现的内容显示设置引用如下:
您的示例:
-(void)setSelected:(BOOL)flag
我不知道语言,但是
BOOL
是一个 ID 吗?注意: 我确实在文档中看到了这一点:
重要提示:在 Mac OS X v10.5 中,NSCollectionViewItem 类的超类是 NSObject。在 Mac OS X v10.6 及更高版本中,NSCollectionViewItem 现在是 NSViewController 的子类。进行此更改是为了改进 NSCollectionView 中视图的复制方式。 NSCollectionViewItem 与以前的实现保持二进制兼容,并且正确处理取消归档。
所以,如果您习惯使用较旧的 API,那么自您上次执行此操作以来可能已经发生了一些变化......???
I found this documentation - NSCollectionViewItem class
What I found there shows setting a reference like so:
Your sample:
-(void)setSelected:(BOOL)flag
I don't know the language but is
BOOL
an id?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 ...???
您应该将自定义视图中的元素绑定到文件所有者(其类应为
CollectionViewItem
)表示的对象。例如,您的图像视图将绑定到文件的所有者,模型键路径为representedObject.imageSource
。NSCollectionViewItem
是NSViewController
的子类。因此,您可以覆盖-loadView
来执行自定义初始化。例如,You should bind elements in your custom view to the file’s owner (whose class should be
CollectionViewItem
) represented object. For instance, your image view would be bound to the file’s owner with the model key path beingrepresentedObject.imageSource
.NSCollectionViewItem
is a subclass ofNSViewController
. As such, you can override-loadView
to perform custom initialisation. For instance,