NSCollectionViewItem & Its View

  • 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 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.
  • On 11/3/07, I. Savant <idiotsavant2005...> 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


    >
  • Doug:

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

      Gah! I completely overlooked that particular initialization vector.
    It works like a charm, thanks!

    --
    I.S.
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today