setRepresentedObject gets nil initially as parameter even if content is set (CPCollectionView)

59 views
Skip to first unread message

Diego Developers

unread,
Jan 10, 2017, 12:32:23 PM1/10/17
to Cappuccino & Objective-J
Hi,

I recently observed a weird behaviour regarding collection view. To further explain this problem consider the Scrapbook part - 2 app from the cappuccino official site.

- (id)init
{
    self = [self initWithContentRect:CGRectMake(0.0, 0.0, 300.0, 400.0)
                           styleMask:CPHUDBackgroundWindowMask |
                                     CPClosableWindowMask |
                                     CPResizableWindowMask];

    if (self)
    {
        [self setTitle:"Photos"];
        [self setFloatingPanel:YES];

        var contentView = [self contentView],
            bounds = [contentView bounds];

        bounds.size.height -= 20.0;

        var photosView = [[CPCollectionView alloc] initWithFrame:bounds];

        [photosView setAutoresizingMask:CPViewWidthSizable];
        [photosView setMinItemSize:CGSizeMake(100, 100)];
        [photosView setMaxItemSize:CGSizeMake(100, 100)];

        var itemPrototype = [[CPCollectionViewItem alloc] init],
            photoView = [[PhotoView alloc] initWithFrame:CGRectMakeZero()];

        [itemPrototype setView:photoView];

        [photosView setItemPrototype:itemPrototype];

        var scrollView = [[CPScrollView alloc] initWithFrame:bounds];

        [scrollView setDocumentView:photosView];
        [scrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
        [scrollView setAutohidesScrollers:YES];

        [[scrollView contentView] setBackgroundColor:[CPColor whiteColor]];

        [contentView addSubview:scrollView];

        images = [  [[CPImage alloc]
                        initWithContentsOfFile:"Resources/sample.jpg"
                                          size:CGSizeMake(500.0, 430.0)],
                    [[CPImage alloc]
                        initWithContentsOfFile:"Resources/sample2.jpg"
                                          size:CGSizeMake(500.0, 389.0)],
                    [[CPImage alloc]
                        initWithContentsOfFile:"Resources/sample3.jpg"
                                          size:CGSizeMake(413.0, 400.0)],
                    [[CPImage alloc]
                        initWithContentsOfFile:"Resources/sample4.jpg"
                                          size:CGSizeMake(500.0, 375.0)],
                    [[CPImage alloc]
                        initWithContentsOfFile:"Resources/sample5.jpg"
                                          size:CGSizeMake(500.0, 375.0)],
                    [[CPImage alloc]
                        initWithContentsOfFile:"Resources/sample6.jpg"
                                          size:CGSizeMake(500.0, 375.0)]];

        [photosView setContent:images];
    }

    return self;
}

@implementation PhotoView : CPView
{
    CPImageView _imageView;
}

- (void)setRepresentedObject:(id)anObject
{
    if (!_imageView)
    {
        var frame = CGRectInset([self bounds], 5.0, 5.0);

        _imageView = [[CPImageView alloc] initWithFrame:frame];

        [_imageView setImageScaling:CPScaleProportionally];
        [_imageView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];

        [self addSubview:_imageView];
    }

    [_imageView setImage:anObject];
}

- (void)setSelected:(BOOL)isSelected
{
    [self setBackgroundColor:isSelected ? [CPColor grayColor] : nil];
}

@end

So in the above example the content array has 6 image objects, but the setRepresentedObject method of PhotoView is called 12 times, with nil and content being set. Here's a console log for better understanding:

2017-01-10 10:17:31.994 Cappuccino [warn]: anObject null
2017-01-10 10:17:38.191 Cappuccino [warn]: anObject <CPImage 0x009b09> {
   filename: "Resources/sample.jpg",
   size: { width:500, height:430 }
}
2017-01-10 10:17:38.733 Cappuccino [warn]: anObject null
2017-01-10 10:17:39.733 Cappuccino [warn]: anObject <CPImage 0x009b0b> {
   filename: "Resources/sample2.jpg",
   size: { width:500, height:389 }
}
2017-01-10 10:17:39.736 Cappuccino [warn]: anObject null
2017-01-10 10:17:39.737 Cappuccino [warn]: anObject <CPImage 0x009b0d> {
   filename: "Resources/sample3.jpg",
   size: { width:413, height:400 }
}
2017-01-10 10:17:39.739 Cappuccino [warn]: anObject null
2017-01-10 10:17:39.740 Cappuccino [warn]: anObject <CPImage 0x009b0f> {
   filename: "Resources/sample4.jpg",
   size: { width:500, height:375 }
}
2017-01-10 10:17:39.742 Cappuccino [warn]: anObject null
2017-01-10 10:17:39.744 Cappuccino [warn]: anObject <CPImage 0x009b11> {
   filename: "Resources/sample5.jpg",
   size: { width:500, height:375 }
}
2017-01-10 10:17:39.745 Cappuccino [warn]: anObject null
2017-01-10 10:17:39.746 Cappuccino [warn]: anObject <CPImage 0x009b13> {
   filename: "Resources/sample6.jpg",
   size: { width:500, height:375 }
}

Is this a valid behaviour or am i missing something ?

Keary Suska

unread,
Jan 10, 2017, 2:19:29 PM1/10/17
to objec...@googlegroups.com

> On Jan 9, 2017, at 10:06 PM, Diego Developers <diego.de...@robosoftin.com> wrote:
>
> Hi,
>
> I recently observed a weird behaviour regarding collection view. To further explain this problem consider the Scrapbook part - 2 app from the cappuccino official site.
>
>
It is the implemented behavior, whether it is normal or not ;-) The issue is that when the CPCollectionViewItem prototype is copied, the copy method calls setRepresentedObject (which is likely nil), then CPCollectionView calls setRepresentedObject on the new item with the proper content object. In both cases, the item’s setRepresentedObject calls the represented object’s setRepresentedObject, if implemented.

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"

Cacaodev cacaodev

unread,
Jan 12, 2017, 6:49:13 AM1/12/17
to Cappuccino & Objective-J

Is this a valid behaviour or am i missing something ?

Hi Diego,
Can you check the cocoa behavior and open an issue if it differs from capp ?

A solution could be either to nil check the representedObject in CPCollectionViewItem -copy or add an equality check directly in setRepresentedObject: that would stop the propagation to the associated view.

Diego Developers

unread,
Jan 16, 2017, 7:07:09 AM1/16/17
to Cappuccino & Objective-J
Hi @Cacaodev @Keary Suska, sorry for the late reply. I have created a sample mac osx app to test the cocoa behaviour as mentioned by @Cacaodev. And it seems that cappuccino is following the same 
exact behaviour as cocoa, mentioned by @Keary Suska. Meaning both (Cocoa and Cappuccino) send nil initially due to copy method and then send the appropriate content. Next time I'll be sure to check the cocoa behaviour before posting rookie questions :P Thanks for the all help :)
Reply all
Reply to author
Forward
0 new messages