Skip to content

Instantly share code, notes, and snippets.

@ryanfitz
Created June 16, 2015 21:56
asdk table view manager
//
// ASTableViewManager.swift
// cinch
//
// Created by Ryan Fitzgerald on 3/9/15.
// Copyright (c) 2015 cinch. All rights reserved.
//
import Foundation
import RFSectionDelta
import INSPullToRefresh
class ASTableViewManager {
var batchContext : ASBatchContext?
init() {
}
func attachPullToRefresh(tableView : ASTableView, handler : (scrollView : UIScrollView!) -> Void ) {
tableView.ins_addPullToRefreshWithHeight(48.0, handler : handler)
let defaultFrame = CGRectMake(0, 0, 24, 24)
let pullToRefresh = INSDefaultPullToRefresh(frame: defaultFrame, backImage: nil, frontImage: UIImage(named : "pullToRefreshIcon"))
tableView.ins_pullToRefreshBackgroundView.delegate = pullToRefresh
tableView.ins_pullToRefreshBackgroundView.addSubview(pullToRefresh)
}
func update(tableView: ASTableView, removedSections: NSIndexSet?, insertedSections: NSIndexSet?, movedSections: [MovedIndex]?) {
var del = NSMutableIndexSet()
var add = NSMutableIndexSet()
if let remove = removedSections {
del.addIndexes(remove)
}
if let inserts = insertedSections {
add.addIndexes(inserts)
}
if del.count == 0 && add.count == 0 {
if let moves = movedSections {
for move in moves.reverse() {
del.addIndex(move.oldIndex)
add.addIndex(move.newIndex)
}
}
}
tableView.beginUpdates()
if del.count > 0 {
tableView.deleteSections(del, withRowAnimation: .None)
}
if add.count > 0 {
tableView.insertSections(add, withRowAnimation: .None)
}
tableView.endUpdates()
if let context = self.batchContext {
context.completeBatchFetching(true)
self.batchContext = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment