Last active
February 26, 2018 01:15
-
-
Save acrookston/c47c4131adb462d0ddf7 to your computer and use it in GitHub Desktop.
UICollectionView example in Swift
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
class HeaderCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate { | |
var collectionView : UICollectionView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
var layout = CSStickyHeaderFlowLayout() | |
layout.parallaxHeaderReferenceSize = CGRectMake(... size of the header...) | |
layout.minimumInteritemSpacing = 0 | |
layout.minimumLineSpacing = 0 | |
self.collectionView = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout: layout) | |
self.collectionView!.registerClass(ShotPreviewCell.self, forCellWithReuseIdentifier: "shotPreview") | |
self.collectionView!.delegate = self | |
self.collectionView!.dataSource = self | |
self.collectionView!.registerClass(ProfileViewHeader.self, forSupplementaryViewOfKind: CSStickyHeaderParallaxHeader, withReuseIdentifier: "ProfileViewHeader") | |
self.view.addSubview(self.collectionView!) | |
} | |
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return (self.shots != nil) ? self.shots!.count : 0 | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { | |
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("shotPreview", forIndexPath: indexPath) as ShotPreviewCell | |
// update cell | |
return cell | |
} | |
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { | |
if kind == CSStickyHeaderParallaxHeader { | |
var headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "ProfileViewHeader", forIndexPath: indexPath) as ProfileViewHeader | |
headerView.setup(self.user!) | |
return headerView | |
} | |
return UICollectionReusableView() | |
} | |
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { | |
let aspect: CGFloat = UIScreen.mainScreen().bounds.size.width / 3.0 | |
return CGSizeMake(shotWidth * aspect, shotHeight * aspect + 50) | |
} | |
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { | |
var cell = collectionView.cellForItemAtIndexPath(indexPath) as ShotPreviewCell | |
if (cell.image == nil) { | |
return | |
} | |
// do something when selected | |
} | |
} |
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
pod 'CSStickyHeaderFlowLayout' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment