Created
November 1, 2015 12:14
-
-
Save takuoka/764b496d7d21d126ae48 to your computer and use it in GitHub Desktop.
Code only example of https://github.com/ecerney/CollectionViewWaterfallLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CollectionViewWaterfallLayout | |
class WaterfallCollectionViewController: UIViewController, UICollectionViewDataSource, CollectionViewWaterfallLayoutDelegate { | |
var collectionView: UICollectionView! | |
init () { | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
lazy var cellSizes: [CGSize] = { | |
var _cellSizes = [CGSize]() | |
for _ in 0...100 { | |
let random = Int(arc4random_uniform((UInt32(100)))) | |
_cellSizes.append(CGSize(width: 140, height: 50 + random)) | |
} | |
return _cellSizes | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let layout = CollectionViewWaterfallLayout() | |
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) | |
layout.headerInset = UIEdgeInsetsMake(20, 0, 0, 0) | |
layout.headerHeight = 50 | |
layout.footerHeight = 20 | |
layout.minimumColumnSpacing = 10 | |
layout.minimumInteritemSpacing = 10 | |
self.collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout) | |
view.addSubview(collectionView) | |
self.collectionView.dataSource = self | |
self.collectionView.delegate = self | |
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") | |
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: CollectionViewWaterfallElementKindSectionHeader, withReuseIdentifier: "Header") | |
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: CollectionViewWaterfallElementKindSectionFooter, withReuseIdentifier: "Footer") | |
} | |
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { | |
return 1 | |
} | |
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return cellSizes.count | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) | |
cell.backgroundColor = UIColor.whiteColor() | |
if let label = cell.contentView.viewWithTag(1) as? UILabel { | |
label.text = String(indexPath.row) | |
} | |
return cell | |
} | |
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { | |
var reusableView: UICollectionReusableView? = nil | |
if kind == CollectionViewWaterfallElementKindSectionHeader { | |
reusableView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) | |
if let view = reusableView { | |
view.backgroundColor = UIColor.redColor() | |
} | |
} | |
else if kind == CollectionViewWaterfallElementKindSectionFooter { | |
reusableView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) | |
if let view = reusableView { | |
view.backgroundColor = UIColor.blueColor() | |
} | |
} | |
return reusableView! | |
} | |
// MARK: WaterfallLayoutDelegate | |
func collectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { | |
return cellSizes[indexPath.item] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment