Skip to content

Commit 0269f92

Browse files
author
Kyle Truscott
committed
Style tweaks to minimumInteritemSpacingForSectionAtIndex updates from @danblakemore
1 parent f2c4bc3 commit 0269f92

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 1.1 - 2015-09-15
6+
7+
* Uses flow layout `minimumInteritemSpacingForSectionAtIndex` from delegate if available. Thanks [@danblakemore](https://github.com/danblakemore)
8+
59
## 1.0.0 - 2015-05-06
610

711
* Fixes issues where variable cell heights on 3x res screen pile up on top of each other

Example/CenterFlow/KTMainController.m

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
115115

116116
#pragma mark - UICollectionViewDelegateFlowLayout
117117

118+
//- (CGFloat)collectionView:(UICollectionView *)cv layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
119+
//{
120+
// return 30.f;
121+
//}
122+
118123
- (CGSize)collectionView:(UICollectionView *)collectionView
119124
layout:(UICollectionViewFlowLayout *)collectionViewLayout
120125
sizeForItemAtIndexPath:(NSIndexPath *)indexPath

KTCenterFlowLayout.m

+11-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
1818

1919
NSMutableDictionary *rowCollections = [NSMutableDictionary new];
2020

21+
id <UICollectionViewDelegateFlowLayout> flowDelegate = (id<UICollectionViewDelegateFlowLayout>) [[self collectionView] delegate];
22+
BOOL delegateSupportsInteritemSpacing = [flowDelegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)];
23+
2124
// Collect attributes by their midY coordinate.. i.e. rows!
22-
2325
for (UICollectionViewLayoutAttributes *itemAttributes in superAttributes)
2426
{
2527
// Normalize the midY to others in the row
@@ -53,21 +55,15 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
5355
NSInteger itemsInRow = [itemAttributesCollection count];
5456

5557
// x-x-x-x ... sum up the interim space
56-
CGFloat interitemSpacing;
57-
if ([[[self collectionView] delegate] respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)])
58+
CGFloat interitemSpacing = [self minimumInteritemSpacing];
59+
60+
// Check for delegate support
61+
if (delegateSupportsInteritemSpacing && itemsInRow > 0)
5862
{
59-
NSUInteger section;
60-
if (itemsInRow) {
61-
UICollectionView * collectionView = [self collectionView];
62-
id <UICollectionViewDelegateFlowLayout> delegate = [collectionView delegate];
63-
64-
// assuming rows never contain elements from more than one section
65-
interitemSpacing = [delegate collectionView:collectionView layout:self minimumInteritemSpacingForSectionAtIndex:[[itemAttributesCollection[0] indexPath] section]];
66-
} else {
67-
interitemSpacing = 0; // probably a sensible default?
68-
}
69-
} else {
70-
interitemSpacing = [self minimumInteritemSpacing];
63+
NSInteger section = [[itemAttributesCollection[0] indexPath] section];
64+
interitemSpacing = [flowDelegate collectionView:self.collectionView
65+
layout:self
66+
minimumInteritemSpacingForSectionAtIndex:section];
7167
}
7268

7369
CGFloat aggregateInteritemSpacing = interitemSpacing * (itemsInRow -1);

0 commit comments

Comments
 (0)