Skip navigation.
 
mlRe: Problem binding to recreated NSCollectionView
FROM : David Carlisle
DATE : Fri May 16 22:18:50 2008

On May 16, 2008, at 11:03 AM, I. Savant wrote:
> On Fri, May 16, 2008 at 12:55 PM, David Carlisle
> <<email_removed>> wrote:

>> FWIW, I got my very own collection view (MyVOCV) working nicely with
>> NSArrayController, all except for animations, and I just pre 
>> ordered the
>> animation book from Amazon.
>> So thanks for the suggestions,

>
>  Hey! Good work. Maybe it's worth posting on CocoaDev.com or a blog
> somewhere. I'm sure a LOT of people would benefit from something like
> that. Sort of a "pay it forward" kind of thing. Congrats.
>
> --
> I.S.


There are only a few "secrets" involved (in my limited and as yet 
unanimated version), and the one about using NSKeyedArchiver is 
already on CocoaDev.  The other one is that if you bind 
MyCollectionView content to NSArrayController arrangedObjects, you 
need to have the MyCollectionView content method return a nil to 
NSArrayController to force it to call setContent with the updated array.

My MyCollectionViewItem in the nib is connected to MyCollectionView 
IBOutlet itemPrototype, and the prototype NSView in the nib is 
connected to MyCollectionViewItem IBOutlet prototypeView.  When I call 
setView in a copy of MyCollectionViewItem, I put that view into a 
different NSView variable so as not to confuse it with prototypeView.

These are some important methods in MyCollectionView:


- (NSView *) copyOfPrototypeView {
    NSData *d = [NSKeyedArchiver archivedDataWithRootObject:
[itemPrototype prototypeView]];
    return [NSKeyedUnarchiver unarchiveObjectWithData:d];
}

- (void) newItemForRepresentedObject:(id)ro {
    MyCollectionViewItem *newProto = [itemPrototype copy];
    [newProto setCollectionView:self];
    [newProto setRepresentedObject:ro];
    NSView *newView = [self copyOfPrototypeView];
    [newProto setView:newView];

    // etc, for example:
    [newView setFrameOrigin:p];
    [self addSubview:newView];
}

- (void) setContent:(NSArray *)content {
  // compare with old content array to do layout and animation
}

- (NSArray *) content {
    return nil;  // This forces NSArrayController to call setContent
}

- (BOOL) isFlipped {
    return YES;
}

// This is probably not needed
- (Class) valueClassForBinding:(NSString *)binding {
    return [NSObject class];
}

Related mailsAuthorDate
mlProblem binding to recreated NSCollectionView David Carlisle May 14, 22:44
mlRe: Problem binding to recreated NSCollectionView David Carlisle May 16, 22:18