How do you initialize a NSCollectionViewItem?

假装没事ソ 提交于 2019-12-07 18:55:05

问题


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

回答1:


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 ...???




回答2:


I cannot link the view back to the NSCollectionViewItem subclass in Interface Builder.

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 being representedObject.imageSource.

Is there an init method I can use with my NSCollectionViewItem in order to perform initialization operations?

NSCollectionViewItem is a subclass of NSViewController. As such, you can override -loadView to perform custom initialisation. For instance,

@implementation CollectionViewItem
…
- (void)loadView {
    [super loadView];
    self.someProperty = someDefaultValue;
}
…
@end


来源:https://stackoverflow.com/questions/6194395/how-do-you-initialize-a-nscollectionviewitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!