Re: NSCollectionViewItem & Its View
Re: NSCollectionViewItem & Its View
- Subject: Re: NSCollectionViewItem & Its View
- From: "Doug Penny" <email@hidden>
- Date: Mon, 5 Nov 2007 10:57:52 -0500
On 11/3/07, I. Savant <email@hidden> wrote:
> List:
>
> I'm hoping someone can clarify something for me. I'm using the
> NSCollectionView/NSCollectionViewItem classes in a pet project of
> mine. The project is "garbage collection required". The item's
> designated view is a simple NSView subclass that has two properties
> defined as:
>
> BOOL connected;
> NSColor * statusColor;
>
> I dutifully initialized the statusColor and connected ivars in the
> view's -initWithFrame: method (to "NO" and "[NSColor redColor]",
> respectively). A strange thing happens, however, when -drawRect: is
> called. Most of my custom drawing shows up just fine (for the
> background) but the statusColor ivar is nil. Also, if I change the
> initialization to set the "connected" ivar to "YES", this value is
> ignored (it evaluates to "NO");
>
> I discovered -initWithFrame: is never called. Okay, so I moved the
> code to -awakeFromNib, which is called once, but I get the same
> results - my ivars appear uninitialized when used in -drawRect: ...
>
> My first suspicion was there was something about garbage collection
> that I do not understand but the view draws fine if I instantiate one
> of them in a separate window that stays on-screen. I've also been
> using GC in plenty of other classes with the exact same patterns. It's
> only happening when used by a collection view item.
>
> That lead me to suspect something is 'different' about how the
> collection view / item handles its views (since the "prototype view"
> theoretically is copied). Maybe it calls -init and not -initWithFrame:
> (odd, but worth a shot). Nope. Never called. Nor is -copy: ...
I think you will need to use -initWithCoder: for this. My
understanding is that any subclass used for a collection view item
must conform to the NSCoding protocol.
>
> I thought maybe the prototype view itself should just be a NSView.
> I enclosed all the controls in a subview and made *that* my subclass
> (turning the prototype view back into a stock NSView). No luck.
>
> The documentation is still rather sparse (I know Scott and mmalc,
> you guys had a lot to write for Leopard and detail will take time) so
> beyond the basics, I can't see any explanation for this behavior. I'm
> sure I'm missing something obvious but damned if I know what it is.
> Any ideas?
>
> --
> I.S.
>
I don't think this is clearly documented anywhere, I got it from
talking to an Apple engineer. When a NSCollectionView replicates the
prototype view defined in the nib for display in the collection view,
it does so by re-reading that view from the nib. It does this in
order to properly fix up any connections and bindings that the view
may have. Therefore, your class's -initWithCoder: method is invoked,
as opposed to - initWithFrame:. Hope this helps.
~D. Penny
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden