Skip to content

Instantly share code, notes, and snippets.

@gkoehler
Created November 12, 2015 13:23
Show Gist options
  • Save gkoehler/ac63ffbcfd147227c82b to your computer and use it in GitHub Desktop.
Save gkoehler/ac63ffbcfd147227c82b to your computer and use it in GitHub Desktop.
UICollectionView: performBatchUpdates example
//
// ViewController.swift
// collView2
//
// Created by Gavin Koehler on 11/11/15.
// Copyright © 2015 From Now On, LLC. All rights reserved.
//
import UIKit
extension CollectionType where Generator.Element: Hashable {
// Returns the index paths of the elements which still exist after the subtraction
func differenceWith(collectionToSubtract:[Self.Generator.Element]) -> [NSIndexPath] {
return Set(self).subtract(Set(collectionToSubtract))
.flatMap { self.indexOf($0) as? Int }
.map { NSIndexPath(forItem: $0, inSection: 0) }
}
}
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var cellData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0...10 {
cellData.append("Cell #\(i)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func addOnePressed(sender: AnyObject) {
// cellData.insert("New Object", atIndex: 0)
// collectionView.reloadData()
// self.cellData.insert("New Object", atIndex: 0)
// self.collectionView.insertItemsAtIndexPaths([NSIndexPath(forItem: 0, inSection: 0)])
self.cellData.insert("New Object", atIndex: 0)
collectionView.performBatchUpdates({ () -> Void in
self.collectionView.insertItemsAtIndexPaths([NSIndexPath(forItem: 0, inSection: 0)])
}, completion:nil)
}
@IBAction func addMultiple(sender: AnyObject) {
// self.cellData.insertContentsOf(["New Object 0", "New Object 1", "New Object 2"], at: 0)
//
// self.collectionView.insertItemsAtIndexPaths([
// NSIndexPath(forItem: 0, inSection: 0),
// NSIndexPath(forItem: 1, inSection: 0),
// NSIndexPath(forItem: 2, inSection: 0),
// ])
collectionView.performBatchUpdates({ () -> Void in
self.cellData.insertContentsOf(["New Object 0", "New Object 1", "New Object 2"], at: 0)
self.collectionView.insertItemsAtIndexPaths([
NSIndexPath(forItem: 0, inSection: 0),
NSIndexPath(forItem: 1, inSection: 0),
NSIndexPath(forItem: 2, inSection: 0),
])
}, completion: nil)
}
@IBAction func addSubtractMultiple(sender: AnyObject) {
// 1. generate new data
// 2. get index paths of new data vs. old data
// 3. assign new data
// 4. perform batch updates
var newData = [String]()
for i in 1...12 {
newData.append("Cell #\(i)")
}
let subtracted = Set(cellData).subtract(Set(newData))
let added = Set(newData).subtract(Set(cellData))
print("Subtracted: \(subtracted)")
print("Added: \(added)")
let subtractedIndexPaths = cellData.differenceWith(newData)
let addedIndexPaths = newData.differenceWith(cellData)
print("subtractedIndexPaths: \(subtractedIndexPaths)")
print("addedIndexPaths: \(addedIndexPaths)")
cellData = newData
collectionView.performBatchUpdates({ () -> Void in
self.collectionView.deleteItemsAtIndexPaths(subtractedIndexPaths)
self.collectionView.insertItemsAtIndexPaths(addedIndexPaths)
}, completion: nil)
}
@IBAction func resetPressed(sender: AnyObject) {
let newData = ["Item 1", "item 2"]
let subtractedIndexPaths = cellData.differenceWith(newData)
let addedIndexPaths = newData.differenceWith(cellData)
cellData = newData
collectionView.performBatchUpdates({ () -> Void in
self.collectionView.deleteItemsAtIndexPaths(subtractedIndexPaths)
self.collectionView.insertItemsAtIndexPaths(addedIndexPaths)
}, completion: nil)
}
}
extension ViewController : UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellData.count
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! LabelCellCollectionViewCell
cell.label.text = cellData[indexPath.item]
return cell
}
}
extension ViewController : UICollectionViewDelegate {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment