Skip to content

Commit

Permalink
Fixes an regression with the detection if we should update the layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
steipete committed Jul 16, 2013
1 parent 87f152d commit bc79b0d
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions PSTCollectionView/PSTCollectionViewFlowLayout.m
Expand Up @@ -92,7 +92,7 @@ - (id)initWithCoder:(NSCoder *)decoder {
if ((self = [super initWithCoder:decoder])) {
[self commonInit];

// some properties are not set if they're default (like minimumInteritemSpacing == 10)
// Some properties are not set if they're default (like minimumInteritemSpacing == 10)
if ([decoder containsValueForKey:@"UIItemSize"])
self.itemSize = [decoder decodeCGSizeForKey:@"UIItemSize"];
if ([decoder containsValueForKey:@"UIInteritemSpacing"])
Expand All @@ -113,7 +113,6 @@ - (id)initWithCoder:(NSCoder *)decoder {

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];

[coder encodeCGSize:self.itemSize forKey:@"UIItemSize"];
[coder encodeFloat:self.minimumInteritemSpacing forKey:@"UIInteritemSpacing"];
[coder encodeFloat:self.minimumLineSpacing forKey:@"UILineSpacing"];
Expand All @@ -130,10 +129,7 @@ - (void)encodeWithCoder:(NSCoder *)coder {

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
// Apple calls _layoutAttributesForItemsInRect

if (!_data) {
[self prepareLayout];
}
if (!_data) [self prepareLayout];

NSMutableArray *layoutAttributesArray = [NSMutableArray array];
for (PSTGridLayoutSection *section in _data.sections) {
Expand Down Expand Up @@ -200,9 +196,7 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
}

- (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
if (!_data) {
[self prepareLayout];
}
if (!_data) [self prepareLayout];

PSTGridLayoutSection *section = _data.sections[indexPath.section];
PSTGridLayoutRow *row = nil;
Expand Down Expand Up @@ -231,38 +225,30 @@ - (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInd
}

- (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if (!_data) {
[self prepareLayout];
}
if (!_data) [self prepareLayout];

NSUInteger sectionIndex = indexPath.section;

PSTCollectionViewLayoutAttributes *layoutAttributes = nil;

if (sectionIndex < _data.sections.count) {
PSTGridLayoutSection *section = _data.sections[sectionIndex];

CGRect normilazedFrame = CGRectZero;

CGRect normalizedFrame = CGRectZero;
if ([kind isEqualToString:PSTCollectionElementKindSectionHeader]) {
normilazedFrame = section.headerFrame;
normalizedFrame = section.headerFrame;
}
else if ([kind isEqualToString:PSTCollectionElementKindSectionFooter]) {
normilazedFrame = section.footerFrame;
normalizedFrame = section.footerFrame;
}

if (!CGRectIsEmpty(normilazedFrame)) {
normilazedFrame.origin.x += section.frame.origin.x;
normilazedFrame.origin.y += section.frame.origin.y;
if (!CGRectIsEmpty(normalizedFrame)) {
normalizedFrame.origin.x += section.frame.origin.x;
normalizedFrame.origin.y += section.frame.origin.y;

layoutAttributes = [[self.class layoutAttributesClass] layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:[NSIndexPath indexPathForItem:0 inSection:sectionIndex]];
layoutAttributes.frame = normilazedFrame;

layoutAttributes.frame = normalizedFrame;
}


}

return layoutAttributes;
}

Expand All @@ -271,11 +257,8 @@ - (PSTCollectionViewLayoutAttributes *)layoutAttributesForDecorationViewWithReus
}

- (CGSize)collectionViewContentSize {
if (!_data) {
[self prepareLayout];
}
if (!_data) [self prepareLayout];

// return _currentLayoutSize;
return _data.contentSize;
}

Expand All @@ -297,7 +280,8 @@ - (void)invalidateLayout {

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
// we need to recalculate on width changes
if ((self.collectionView.bounds.size.width != newBounds.size.width && self.scrollDirection == PSTCollectionViewScrollDirectionVertical) || (self.collectionView.bounds.size.height != newBounds.size.height && self.scrollDirection == PSTCollectionViewScrollDirectionHorizontal)) {
if ((_visibleBounds.size.width != newBounds.size.width && self.scrollDirection == PSTCollectionViewScrollDirectionVertical) || (_visibleBounds.size.height != newBounds.size.height && self.scrollDirection == PSTCollectionViewScrollDirectionHorizontal)) {
_visibleBounds = self.collectionView.bounds;
return YES;
}
return NO;
Expand All @@ -314,7 +298,8 @@ - (void)prepareLayout {

_data = [PSTGridLayoutInfo new]; // clear old layout data
_data.horizontal = self.scrollDirection == PSTCollectionViewScrollDirectionHorizontal;
CGSize collectionViewSize = self.collectionView.bounds.size;
_visibleBounds = self.collectionView.bounds;
CGSize collectionViewSize = _visibleBounds.size;
_data.dimension = _data.horizontal ? collectionViewSize.height : collectionViewSize.width;
_data.rowAlignmentOptions = _rowAlignmentsOptionsDictionary;
[self fetchItemsInfo];
Expand Down

0 comments on commit bc79b0d

Please sign in to comment.