Last active
August 29, 2015 14:18
-
-
Save alexbosworth/6bbf2ce9aa59fbbd5a93 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
// | |
// TableModifications.swift | |
// mn_ios | |
// | |
// Created by Alex Bosworth on 4/8/15. | |
// Copyright (c) 2015 adylitica. All rights reserved. | |
// | |
import UIKit | |
/** Modifications to a table view | |
*/ | |
struct TableModifications<T: Equatable where T: Hashable> { | |
// MARK: - Init Properties | |
let previousSections: [T] | |
// MARK: - Mutable Properties | |
private var _finalSections = [T]() | |
var sectionsToInsert: Set<T> = Set() | |
var sectionsToRemove: Set<T> = Set() | |
var pathsToConfigure: Set<NSIndexPath> = Set() | |
var pathsToInsert: Set<NSIndexPath> = Set() | |
var pathsToMove: [(from: NSIndexPath, to: NSIndexPath)] = [] | |
var pathsToRemove: Set<NSIndexPath> = Set() | |
// MARK: - Generated Properties | |
var sectionsToInsertAsNSIndexSet: NSIndexSet { | |
return _indexSetFromSections(sectionsToInsert, referencing: _finalSections) | |
} | |
var sectionsToRemoveAsNSIndexSet: NSIndexSet { | |
return _indexSetFromSections(sectionsToRemove, referencing: previousSections) | |
} | |
// MARK: - Init | |
init(previousSections: [T]) { | |
self.previousSections = previousSections | |
} | |
// MARK: - UITableView Modifications | |
/** Combine these modifications with another set of modifications | |
*/ | |
mutating func importModifications(modifications: TableModifications) { | |
sectionsToInsert.unionInPlace(modifications.sectionsToInsert) | |
sectionsToRemove.unionInPlace(modifications.sectionsToRemove) | |
pathsToConfigure.unionInPlace(modifications.pathsToConfigure) | |
pathsToInsert.unionInPlace(modifications.pathsToInsert) | |
pathsToMove += modifications.pathsToMove | |
pathsToRemove.unionInPlace(modifications.pathsToRemove) | |
} | |
/** Get an NSIndex set from an array of sections | |
*/ | |
private func _indexSetFromSections(sections: Set<T>, referencing: [T]) -> NSIndexSet { | |
var set = NSMutableIndexSet() | |
for sec in sections { if let s = find(referencing, sec) { set.addIndex(s) } } | |
return set | |
} | |
/** Modify table sections and cells | |
Note: be sure to call this inside of a beginupdates/endupdates block | |
*/ | |
mutating func makeModificationsInTableView(tableView: UITableView, finalSections: [T]) { | |
_finalSections = finalSections | |
tableView.deleteRowsAtIndexPaths(Array(pathsToRemove), withRowAnimation: .Automatic) | |
tableView.deleteSections(sectionsToRemoveAsNSIndexSet, withRowAnimation: .Automatic) | |
tableView.insertSections(sectionsToInsertAsNSIndexSet, withRowAnimation: .Automatic) | |
tableView.insertRowsAtIndexPaths(Array(pathsToInsert), withRowAnimation: .Automatic) | |
for path in pathsToMove { tableView.moveRowAtIndexPath(path.from, toIndexPath: path.to) } | |
let cells = Array(pathsToConfigure).map { tableView.cellForRowAtIndexPath($0) as? Configurable } | |
for c in cells { c?.configure() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment