Created
May 14, 2015 01:35
-
-
Save keybuk/a387bb8fa09f81792eca to your computer and use it in GitHub Desktop.
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: 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