@@ -42,6 +42,7 @@ @interface RAMCollectionViewFlemishBondLayout ()
4242@property (nonatomic , readonly ) CGFloat cellWidth;
4343@property (nonatomic , readonly ) CGFloat cellHeight;
4444@property (nonatomic , readonly ) NSInteger numberOfSections;
45+ @property (nonatomic , readonly ) NSInteger totalGroupsInCollectionView;
4546@property (nonatomic , readonly ) CGFloat totalHeaderHeight;
4647@property (nonatomic , readonly ) CGFloat totalFooterHeight;
4748@property (nonatomic , assign ) RAMCollectionViewFlemishBondLayoutGroupDirection highlightedCellDirection;
@@ -87,6 +88,18 @@ - (NSInteger)numberOfSections
8788 return [self .collectionView numberOfSections ];
8889}
8990
91+ - (NSInteger )totalGroupsInCollectionView
92+ {
93+ NSInteger totalGroups = 0 ;
94+ for (NSInteger section = 0 ; section < self.numberOfSections ; section++) {
95+ NSIndexPath *indexPath = [NSIndexPath indexPathForItem: 0 inSection: section];
96+
97+ totalGroups += [self totalGroupsAtIndexPath: indexPath];
98+ }
99+
100+ return totalGroups;
101+ }
102+
90103- (CGFloat)totalHeaderHeight
91104{
92105 CGFloat totalHeight = 0 .0f ;
@@ -150,8 +163,9 @@ - (void)prepareLayout
150163 NSIndexPath *indexPath = [NSIndexPath indexPathForItem: item inSection: section];
151164
152165 if (indexPath.item == 0 ) {
153- if ([self .delegate conformsToProtocol: @protocol (RAMCollectionViewFlemishBondLayoutDelegate)] && [self .delegate respondsToSelector: @selector (collectionView:layout:estimatedSizeForHeaderInSection: )]) {
154- CGSize size = [self .delegate collectionView: self .collectionView layout: self estimatedSizeForHeaderInSection: section];
166+ CGSize size = [self estimatedSizeForHeaderInSection: section];
167+
168+ if (!CGSizeEqualToSize (size, CGSizeZero)) {
155169 self.headerSizes [indexPath] = [NSValue valueWithCGSize: size];
156170
157171 UICollectionViewLayoutAttributes *headerAttributes = [UICollectionViewLayoutAttributes
@@ -162,8 +176,9 @@ - (void)prepareLayout
162176 headerLayoutDictionary[indexPath] = headerAttributes;
163177 }
164178 } else if ([self isTheLastItemAtIndexPath: indexPath]) {
165- if ([self .delegate conformsToProtocol: @protocol (RAMCollectionViewFlemishBondLayoutDelegate)] && [self .delegate respondsToSelector: @selector (collectionView:layout:estimatedSizeForFooterInSection: )]) {
166- CGSize size = [self .delegate collectionView: self .collectionView layout: self estimatedSizeForFooterInSection: section];
179+ CGSize size = [self estimatedSizeForFooterInSection: section];
180+
181+ if (!CGSizeEqualToSize (size, CGSizeZero)) {
167182 self.footerSizes [indexPath] = [NSValue valueWithCGSize: size];
168183
169184 UICollectionViewLayoutAttributes *footerAttributes = [UICollectionViewLayoutAttributes
@@ -226,7 +241,7 @@ - (CGSize)collectionViewContentSize
226241 return CGSizeZero;
227242 }
228243
229- return CGSizeMake (self.collectionView .bounds .size .width , (self.highlightedCellHeight * self.numberOfSections * [ self totalGroupsInCollectionView ] ) + self.totalHeaderHeight + self.totalFooterHeight );
244+ return CGSizeMake (self.collectionView .bounds .size .width , (self.highlightedCellHeight * self.totalGroupsInCollectionView ) + self.totalHeaderHeight + self.totalFooterHeight );
230245}
231246
232247- (BOOL )shouldInvalidateLayoutForBoundsChange : (CGRect)newBounds
@@ -271,23 +286,21 @@ - (CGRect)frameForCellAtIndexPath:(NSIndexPath *)indexPath
271286
272287- (CGRect)frameForHeaderAtIndexPath : (NSIndexPath *)indexPath withSize : (CGSize)size
273288{
274- CGRect frame = [ self frameForCellAtIndexPath: indexPath] ;
275- if (frame. origin . y == size. height ) {
289+ CGRect frame = CGRectZero ;
290+ if (indexPath. section == 0 ) {
276291 frame.origin .y = 0 ;
277292 } else {
278- frame.origin .y -= frame. size .height ;
293+ frame.origin .y = [ self getYAtIndexPath: indexPath] - size.height ;
279294 }
280- frame.origin .x = 0 .0f ;
281295 frame.size = size;
282296
283297 return frame;
284298}
285299
286300- (CGRect)frameForFooterAtIndexPath : (NSIndexPath *)indexPath withSize : (CGSize)size
287301{
288- CGRect frame = [self frameForCellAtIndexPath: indexPath];
289- frame.origin .y += self.highlightedCellHeight ;
290- frame.origin .x = 0 .0f ;
302+ CGRect frame = CGRectZero;
303+ frame.origin .y = [self getYAtIndexPath: indexPath] + self.highlightedCellHeight ;
291304 frame.size = size;
292305
293306 return frame;
@@ -312,10 +325,49 @@ - (CGFloat)getYAtIndexPath:(NSIndexPath *)indexPath
312325
313326 yValue = ((currentGroup - 1 ) * self.highlightedCellHeight ) + (self.cellHeight * position) + [self heightHeaderAtIndexPath: indexPathFirstElementCurrentSection];
314327 }
328+
329+ if (indexPath.section > 0 ) {
330+ yValue += (self.highlightedCellHeight * indexPath.section * [self totalGroupsAtIndexPath: indexPath]) + [self headerAndFooterHeightsPreviouslyAtIndexPath: indexPath];
331+ }
315332
316333 return yValue;
317334}
318335
336+ - (CGFloat)headerAndFooterHeightsPreviouslyAtIndexPath : (NSIndexPath *)indexPath
337+ {
338+ CGFloat totalHeight = 0 .f ;
339+ for (NSInteger section = 0 ; section < indexPath.section ; section++) {
340+ CGSize sizeHeader = [self estimatedSizeForHeaderInSection: section];
341+ CGSize sizeFooter = [self estimatedSizeForFooterInSection: section];
342+
343+ totalHeight += sizeHeader.height + sizeFooter.height ;
344+ }
345+
346+ return totalHeight;
347+ }
348+
349+ - (CGSize)estimatedSizeForHeaderInSection : (NSInteger )section
350+ {
351+ CGSize size = CGSizeZero;
352+
353+ if ([self .delegate conformsToProtocol: @protocol (RAMCollectionViewFlemishBondLayoutDelegate)] && [self .delegate respondsToSelector: @selector (collectionView:layout:estimatedSizeForHeaderInSection: )]) {
354+ size = [self .delegate collectionView: self .collectionView layout: self estimatedSizeForHeaderInSection: section];
355+ }
356+
357+ return size;
358+ }
359+
360+ - (CGSize)estimatedSizeForFooterInSection : (NSInteger )section
361+ {
362+ CGSize size = CGSizeZero;
363+
364+ if ([self .delegate conformsToProtocol: @protocol (RAMCollectionViewFlemishBondLayoutDelegate)] && [self .delegate respondsToSelector: @selector (collectionView:layout:estimatedSizeForFooterInSection: )]) {
365+ size = [self .delegate collectionView: self .collectionView layout: self estimatedSizeForFooterInSection: section];
366+ }
367+
368+ return size;
369+ }
370+
319371- (BOOL )isHighLightedElementAtIndexPath : (NSIndexPath *)indexPath
320372{
321373 if ((indexPath.row % self.numberOfElements ) == 0 ) {
@@ -357,18 +409,6 @@ - (NSInteger)totalGroupsAtIndexPath:(NSIndexPath *)indexPath
357409 return resultValue;
358410}
359411
360- - (NSInteger )totalGroupsInCollectionView
361- {
362- NSInteger totalGroups = 0 ;
363- for (NSInteger section = 0 ; section < self.numberOfSections ; section++) {
364- NSIndexPath *indexPath = [NSIndexPath indexPathForItem: 0 inSection: section];
365-
366- totalGroups += [self totalGroupsAtIndexPath: indexPath];
367- }
368-
369- return totalGroups;
370- }
371-
372412- (CGFloat)heightHeaderAtIndexPath : (NSIndexPath *)indexPath
373413{
374414 CGSize size = [self .headerSizes[indexPath] CGSizeValue ];
0 commit comments