Created
June 18, 2014 07:33
-
-
Save kaiinui/f26907d88e1a7f69ef34 to your computer and use it in GitHub Desktop.
Infinite Scroll with UICollectionView 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
import UIKit | |
class ViewController: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate { | |
override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int { | |
... | |
} | |
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! { | |
... | |
} | |
override func scrollViewDidScroll(scrollView: UIScrollView) { | |
let offsetY = scrollView.contentOffset.y | |
let contentHeight = scrollView.contentSize.height | |
if offsetY > contentHeight - scrollView.frame.size.height { | |
loadSomeDataAndIncreaseDataLengthFunction() | |
self.collectionView.reloadData() | |
} | |
} | |
} | |
Not only use it for infinite scroll, you can prefetch images / data before they appear.
Infinite scroll doing with following link
https://webappcodes.com/infinite-scroll-in-collectionview-swift-programmatically
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As UICollectionView inherits UIScrollView, you can write delegate in UICollectionViewController class.