Skip to content

Instantly share code, notes, and snippets.

@keybuk
Created May 14, 2015 01:35
Show Gist options
  • Save keybuk/a387bb8fa09f81792eca to your computer and use it in GitHub Desktop.
Save keybuk/a387bb8fa09f81792eca to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView?
var data = [ "πŸ…", "🍍", "🍎" ]
override func viewDidLoad() {
super.viewDidLoad()
func I(item: Int) -> NSIndexPath { return NSIndexPath(forItem: item, inSection: 0) }
// Data contains three items.
// 0: πŸ…
// 1: 🍍
// 2: 🍎
dispatch_async(dispatch_get_main_queue()) {
// Insert items into data, and reload the others; this is the operation we're describing:
// 0: M πŸ…
// 1: A πŸ†
// 2: A 🌽
// 3: A 🍠
// 4: A πŸ‡
// 5: A 🍈
// 6: A πŸ‰
// 7: A 🍊
// 8: A πŸ‹
// 9: A 🍌
// 10: M 🍍
// 11: M 🍎
self.data = [ "πŸ…", "πŸ†", "🌽", "🍠", "πŸ‡", "🍈", "πŸ‰", "🍊", "πŸ‹", "🍌", "🍍", "🍎" ]
self.collectionView?.performBatchUpdates({ () -> Void in
self.collectionView?.insertItemsAtIndexPaths([ I(1), I(2), I(3), I(4), I(5), I(6), I(7), I(8), I(9) ])
self.collectionView?.reloadItemsAtIndexPaths([ I(0), I(10), I(11) ])
}, completion: nil)
}
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! Cell
cell.label?.text = data[indexPath.item]
return cell
}
}
class Cell: UICollectionViewCell {
@IBOutlet weak var label: UILabel?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment