Skip to content

Commit

Permalink
combine insertions or deletion group
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Feb 15, 2015
1 parent 60863c1 commit adaf027
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 54 deletions.
25 changes: 18 additions & 7 deletions Example/MYTableViewManager/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,34 @@ class ViewController: UIViewController {
self?.pushChildViewController()
}
}
tvm[0].append(cvm)
.fire(.Left)

tvm[0][index*2]?.userData = "new title: \(index)"
tvm[0].insert(cvm[0], atIndex: 0)
//.fire(.Left)
tvm[0].append(cvm[1])

//tvm[0][index*2]?.userData = "new title: \(index)"
//tvm[0][index + 1]?.fire()
}
//tvm[0].remove(0)

for index in 0...4 {
tvm[0].remove(index+1)
.fire(.Right)
/*
delay(2) {
for index in 0...4 {
self.tvm[0].remove(index+1)
//.fire(.Right)
}
}
*/

println("finish setting")
delay(2) {
self.tvm[0].fire()
return
}

delay(7) {
//self.tvm[0].fire()
return
}
/*
let longTitle1 = "Don't have to write the code for UITableViewDelegate and UITableViewDataSource protocols"
let longTitle2 = "Support dynamic cell height from ios7"
Expand Down
48 changes: 15 additions & 33 deletions Source/MYReloadTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ enum ReloadState {

class MYReloadTracker {
var state: ReloadState = .Begin
private var addedIndexes: [Int] = []
private var originalIndexes: [Int] = []
private var removedIndexes: [Int] = []

init() {
didFire()
didFire(0)
}

func didReset() {
Expand All @@ -30,13 +30,8 @@ class MYReloadTracker {
state = .Reset
return
}
for i in range {
var newIndexes: [Int] = []
if addedIndexes.indexOf(i) == nil {
newIndexes.append(i)
}
addedIndexes += newIndexes
}
let ind = range.map { _ -> Int in -1 }
originalIndexes.insert(ind, atIndex: range.startIndex)
state = .Add
}

Expand All @@ -45,7 +40,9 @@ class MYReloadTracker {
state = .Reset
return
}
for i in range {
let ri = originalIndexes[range]
originalIndexes.remove(range)
for i in ri {
var newIndexes: [Int] = []
if removedIndexes.indexOf(i) == nil {
newIndexes.append(i)
Expand All @@ -55,15 +52,21 @@ class MYReloadTracker {
state = .Remove
}

func didFire() {
func didFire(count: Int) {
state = .Begin
addedIndexes = []
originalIndexes = (0..<count).map { $0 }
removedIndexes = []
}

func getIndexPaths(section: Int) -> [NSIndexPath] {
switch state {
case .Add:
var addedIndexes: [Int] = []
originalIndexes.each { i, value in
if value == -1 {
addedIndexes.append(i)
}
}
return addedIndexes.map { NSIndexPath(forRow: $0, inSection: section) }
case .Remove:
return removedIndexes.map { NSIndexPath(forRow: $0, inSection: section) }
Expand All @@ -72,25 +75,4 @@ class MYReloadTracker {
}
return []
}

func getSectionIndexSet() -> NSIndexSet {
switch state {
case .Add:
if addedIndexes.count == 0 {
return NSIndexSet()
}
let min = minElement(addedIndexes)
let max = maxElement(addedIndexes)
return NSIndexSet(indexesInRange: NSRange(min...max))
case .Remove:
if removedIndexes.count == 0 {
return NSIndexSet()
}
let min = minElement(removedIndexes)
let max = maxElement(removedIndexes)
return NSIndexSet(indexesInRange: NSRange(min...max))
default:
return NSIndexSet()
}
}
}
2 changes: 1 addition & 1 deletion Source/MYSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public extension MYSection {
delegate?.reloadSections(NSIndexSet(index: index), animation: animation)
break
}
reloadTracker.didFire()
reloadTracker.didFire(self.count)
return self
}

Expand Down
15 changes: 2 additions & 13 deletions Source/MYTableViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,8 @@ public extension MYTableViewManager {
}

func fire(_ animation: MYAnimation = .None) -> Self {
switch reloadTracker.state {
case .Add:
println("ADD section")
tableView?.insertSections(reloadTracker.getSectionIndexSet(), withRowAnimation: animation)
case .Remove:
println("REMOVE section")
tableView?.deleteSections(reloadTracker.getSectionIndexSet(), withRowAnimation: animation)
default:
println("RESET all sections (reload table view data)")
tableView?.reloadData()
break
}
reloadTracker.didFire()
// TODO : implementation
tableView?.reloadData()
return self
}
}
Expand Down

0 comments on commit adaf027

Please sign in to comment.