I am having trouble displaying the cells in a UICollectionView in a consistent manner. The initial display of the cells is correct, however every time the user scrolls past and then back to a group of cells the display is incorrect. Rows should only ever contain 2 or 1 cell(s). 2 cells each taking half the display width, 1 cell taking full width.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [self preferredSizeForIndexPath:indexPath];
}
- (CGSize)preferredSizeForIndexPath:(NSIndexPath *)indexPath {
BOOL isLastObjectInSection = NO;
NSString *sectionKey = [[arrCollectionData[indexPath.section] allKeys] objectAtIndex:0];
DLog(@"SectionKey: %@", sectionKey);
NSArray *arrSection = [arrCollectionData[indexPath.section] objectForKey:sectionKey];
DLog(@"ArrSection: %@", arrSection);
if ( arrSection[indexPath.row] == arrSection.lastObject ) {
if( arrSection.count % 2 != 0 ) {
isLastObjectInSection = YES;
}
}
CGSize cellSize = CGSizeZero;
if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
if (isLastObjectInSection == YES) {
cellSize = CGSizeMake(IPAD_BADGE_WIDTH_LANDSCAPE_WIDE, IPAD_BADGE_HEIGHT_LANDSCAPE_WIDE);
} else {
cellSize = CGSizeMake(IPAD_BADGE_WIDTH_LANDSCAPE, IPAD_BADGE_HEIGHT_LANDSCAPE);
}
} else {
if (isLastObjectInSection == YES) {
cellSize = CGSizeMake(IPAD_BADGE_WIDTH_WIDE, IPAD_BADGE_HEIGHT_WIDE);
} else {
cellSize = CGSizeMake(IPAD_BADGE_WIDTH, IPAD_BADGE_HEIGHT);
}
}
DLog(@"CellSize: %@", NSStringFromCGSize(cellSize));
return cellSize;
}
Below is a sample of the collection data.
Printing description of self->arrCollectionData:
<__NSArrayI 0x94bbc40>(
{
"March 12, 2013" = (
"<FMLeafTimelineContainer: 0x94b2430>",
"<FMLeafTimelineContainer: 0x94b3670>"
);
},
{
"February 25, 2013" = (
"<FMLeafTimelineContainer: 0x94b4500>"
);
},
{
"February 14, 2013" = (
"<FMLeafTimelineContainer: 0x94b48f0>",
"<FMLeafTimelineContainer: 0x94b3a60>"
);
},
{
"February 12, 2013" = (
"<FMLeafTimelineContainer: 0x94b3ce0>",
"<FMLeafTimelineContainer: 0x94b2b00>"
);
},
{
"February 4, 2013" = (
"<FMCommunityTimelineContainer: 0x94b4e90>",
"<FMCommunityTimelineContainer: 0x94b5050>",
"<FMCommunityTimelineContainer: 0x94b5f70>"
);
},
{
"January 30, 2013" = (
"<FMCommunityTimelineContainer: 0x94b6ad0>",
"<FMCommunityTimelineContainer: 0x94b5a90>"
);
},
{
"January 24, 2013" = (
"<FMCommunityTimelineContainer: 0x94b5d00>",
"<FMCommunityTimelineContainer: 0x94b6d90>"
);
},
{
"January 22, 2013" = (
"<FMCommunityTimelineContainer: 0x94b6440>"
);
},
{
"January 21, 2013" = (
"<FMCommunityTimelineContainer: 0x94b6260>",
"<FMCommunityTimelineContainer: 0x94b62e0>",
"<FMCommunityTimelineContainer: 0x94b70c0>",
"<FMCommunityTimelineContainer: 0x94b55a0>",
"<FMCommunityTimelineContainer: 0x94b82d0>",
"<FMCommunityTimelineContainer: 0x94b78b0>"
);
},
{
"December 20, 2012" = (
"<FMCommunityTimelineContainer: 0x94b53f0>"
);
},
{
"December 6, 2012" = (
"<FMCommunityTimelineContainer: 0x94b7200>"
);
},
{
"December 4, 2012" = (
"<FMCommunityTimelineContainer: 0x94b72b0>"
);
},
{
"November 19, 2012" = (
"<FMCommunityTimelineContainer: 0x94b7ae0>"
);
}
)
The pics below all demonstrate the same section of the collection view.
Problem was caused by a subview of the parent having a fixed frame size assigned at instantiation. Overrode setFrame: for UICollectionView subclass to assign values of subview frames and every time a cell is dequeued and requeued call setFrame: to adjust accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With