Skip to content

Instantly share code, notes, and snippets.

@kaiinui
Created June 18, 2014 07:33
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaiinui/f26907d88e1a7f69ef34 to your computer and use it in GitHub Desktop.
Save kaiinui/f26907d88e1a7f69ef34 to your computer and use it in GitHub Desktop.
Infinite Scroll with UICollectionView in Swift
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()
}
}
}
@kaiinui
Copy link
Author

kaiinui commented Jun 18, 2014

As UICollectionView inherits UIScrollView, you can write delegate in UICollectionViewController class.

@kaiinui
Copy link
Author

kaiinui commented Jun 18, 2014

Not only use it for infinite scroll, you can prefetch images / data before they appear.

@User2004
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment