本文介绍了如何初始化NSCollectionViewItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置一个NSCollectionView在个别NSCollectionViewItem视图中有自定义绘图。我有一个图像,我需要在每个视图中绘制,但我不能链接视图回到NSCollectionViewItem子类在Interface Builder。有一个init方法,我可以使用我的NSCollectionViewItem为了执行初始化操作?我试图实现copyWithZone,但我做错了,因为我有一个永恒的循环。目前,我发现使我的连接到视图的唯一机会是选择后更改使用 - (void)setSelected:(BOOL)标志。我想在视图中做我的绘图,但我需要一个来自我的被代表的图像作为我的源。所有我读的是NSCollectionView相关的,看似不完整。

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


推荐答案

我找到此文档 -

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 

您的示例:
- void)setSelected:(BOOL)flag

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

我不知道语言,但是 BOOL / p>

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

将接收者的表示对象
设置为指定的模型对象。
(可在Mac OS X v10.5到
Mac OS X v10.5。)
- (void)setRepresentedObject:(id)object
参数

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

对象

The receiver’s model object.

可用性

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

在NSCollectionView.h中声明

Declared In NSCollectionView.h

注意:我在文档中看到了这一点:

Note: I did see this on the documentation:

重要提示:在Mac OS X v10.5中, NSCollectionViewItem类的超类是NSObject。在Mac OS X v10.6及更高版本中,NSCollectionViewItem现在是NSViewController的子类。进行此更改是为了改进视图在NSCollectionView中的复制方式。 NSCollectionViewItem保持与先前实现的二进制兼容,并且正确处理取消归档。

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.

所以,如果你习惯使用较旧的API,自从你上一次做以来可能有一些变化... ???

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

这篇关于如何初始化NSCollectionViewItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 00:22