Skip to content

Commit

Permalink
add contentHeight method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo Aoyama authored and Ryo Aoyama committed Jul 26, 2014
1 parent 378e9c9 commit 3578139
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5056" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="1Nm-aK-gXn">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="1Nm-aK-gXn">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions RACollectionViewReorderableTripletLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "RACollectionViewReorderableTripletLayout"
s.version = "0.0.8"
s.version = "0.0.9"
s.summary = "The custom collectionView layout that can perform reordering of cells by dragging it."
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.homepage = "https://github.com/ra1028"
s.author = { "ra1028" => "r.fe51028.r@gmail.com" }
s.platform = :ios, '7.0'
s.source = { :git => "https://github.com/ra1028/RACollectionViewReorderableTripletLayout.git", :tag => "0.0.8" }
s.source = { :git => "https://github.com/ra1028/RACollectionViewReorderableTripletLayout.git", :tag => "0.0.9" }
s.requires_arc = true
s.source_files = 'RACollectionViewReorderableTripletLayout/*.{h,m}'
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "RACollectionViewTripletLayout.h"

@protocol RACollectionViewReorderableTripletLayoutDataSource <UICollectionViewDataSource>
@protocol RACollectionViewReorderableTripletLayoutDataSource <RACollectionViewTripletLayoutDatasource>

@optional

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@

@end

@protocol RACollectionViewTripletLayoutDatasource <UICollectionViewDataSource>

@end

@interface RACollectionViewTripletLayout : UICollectionViewFlowLayout

@property (nonatomic, assign) id<RACollectionViewDelegateTripletLayout> delegate;
@property (nonatomic, assign) id<RACollectionViewTripletLayoutDatasource> datasource;
@property (nonatomic, assign, readonly) CGSize largeCellSize;
@property (nonatomic, assign, readonly) CGSize smallCellSize;

//needs override
- (BOOL)shouldUpdateAttributesArray;
- (BOOL)shouldUpdateAttributesArray; //needs override
- (CGFloat)contentHeight;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,59 @@ - (void)prepareLayout
}
}

- (CGFloat)contentHeight
{
CGFloat contentHeight = 0;
NSInteger numberOfSections = self.collectionView.numberOfSections;
CGSize collectionViewSize = self.collectionView.bounds.size;

UIEdgeInsets insets = UIEdgeInsetsZero;
if ([self.delegate respondsToSelector:@selector(insetsForCollectionView:)]) {
insets = [self.delegate insetsForCollectionView:self.collectionView];
}
CGFloat sectionSpacing = 0;
if ([self.delegate respondsToSelector:@selector(sectionSpacingForCollectionView:)]) {
sectionSpacing = [self.delegate sectionSpacingForCollectionView:self.collectionView];
}
CGFloat itemSpacing = 0;
if ([self.delegate respondsToSelector:@selector(minimumInteritemSpacingForCollectionView:)]) {
itemSpacing = [self.delegate minimumInteritemSpacingForCollectionView:self.collectionView];
}
CGFloat lineSpacing = 0;
if ([self.delegate respondsToSelector:@selector(minimumLineSpacingForCollectionView:)]) {
lineSpacing = [self.delegate minimumLineSpacingForCollectionView:self.collectionView];
}

contentHeight += insets.top + insets.bottom + sectionSpacing * (numberOfSections - 1);

CGFloat lastSmallCellHeight = 0;
for (NSInteger i = 0; i < numberOfSections; i++) {
NSInteger numberOfLines = ceil((CGFloat)[self.collectionView numberOfItemsInSection:i] / 3.f);

CGFloat largeCellSideLength = (2.f * (collectionViewSize.width - insets.left - insets.right) - itemSpacing) / 3.f;
CGFloat smallCellSideLength = (largeCellSideLength - itemSpacing) / 2.f;
CGSize largeCellSize = CGSizeMake(largeCellSideLength, largeCellSideLength);
CGSize smallCellSize = CGSizeMake(smallCellSideLength, smallCellSideLength);
if ([self.delegate respondsToSelector:@selector(collectionView:sizeForLargeItemsInSection:)]) {
if (!CGSizeEqualToSize([self.delegate collectionView:self.collectionView sizeForLargeItemsInSection:i], RACollectionViewTripletLayoutStyleSquare)) {
largeCellSize = [self.delegate collectionView:self.collectionView sizeForLargeItemsInSection:i];
smallCellSize = CGSizeMake(collectionViewSize.width - largeCellSize.width - itemSpacing - insets.left - insets.right, (largeCellSize.height / 2.f) - (itemSpacing / 2.f));
}
}
lastSmallCellHeight = smallCellSize.height;
CGFloat largeCellHeight = largeCellSize.height;
CGFloat lineHeight = numberOfLines * (largeCellHeight + lineSpacing) - lineSpacing;
contentHeight += lineHeight;
}

NSInteger numberOfItemsInLastSection = [self.collectionView numberOfItemsInSection:numberOfSections -1];
if ((numberOfItemsInLastSection - 1) % 3 == 0 && (numberOfItemsInLastSection - 1) % 6 != 0) {
contentHeight -= lastSmallCellHeight + itemSpacing;
}

return contentHeight;
}

- (id<RACollectionViewDelegateTripletLayout>)delegate
{
return (id<RACollectionViewDelegateTripletLayout>)self.collectionView.delegate;
Expand Down

0 comments on commit 3578139

Please sign in to comment.