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 ?