11import UIKit
22
3- protocol OriginalTableConfig {
4- var insertTableViewRowAnimation : UITableViewRowAnimation { get set }
5- var deleteTableViewRowAnimation : UITableViewRowAnimation { get set }
6- var reloadTableViewRowAnimation : UITableViewRowAnimation { get set }
3+ protocol TableViewConfig {
4+ var insertAnimation : UITableViewRowAnimation { get set }
5+ var deleteAnimation : UITableViewRowAnimation { get set }
6+ var reloadAnimation : UITableViewRowAnimation { get set }
77
88 var animateSectionHeaders : Bool { get set }
99}
1010
11- class OriginalTable {
11+ class TableViewWrapper {
1212
13- var sections : [ OriginalSection ]
13+ var sections : [ TableSection ]
1414
1515 var tableView : UITableView
1616
1717 var insertIndexPaths : [ IndexPath ]
1818
1919 var deleteIndexPaths : [ IndexPath ]
2020
21- var updateIndexPaths : [ IndexPath ]
21+ var reloadIndexPaths : [ IndexPath ]
2222
23- var config : OriginalTableConfig
23+ var config : TableViewConfig
2424
25- init ( tableView: UITableView , config: OriginalTableConfig ) {
26- sections = Array ( repeating: OriginalSection ( ) , count: tableView. numberOfSections)
25+ init ( tableView: UITableView , config: TableViewConfig ) {
26+ sections = Array ( repeating: TableSection ( ) , count: tableView. numberOfSections)
2727
2828 var totalNumberOfRows : Int = 0
2929 for i in 0 ..< tableView. numberOfSections {
30- let section = OriginalSection ( )
30+ let section = TableSection ( )
3131 let numberOfRows = tableView. numberOfRows ( inSection: i)
3232
3333 totalNumberOfRows += numberOfRows
@@ -36,7 +36,7 @@ class OriginalTable {
3636 let indexPath = IndexPath ( row: ii, section: i)
3737 let cell = tableView. dataSource!. tableView ( tableView, cellForRowAt: indexPath)
3838
39- let row = OriginalRow ( cell: cell, originalIndexPath : indexPath)
39+ let row = TableRow ( cell: cell, indexPath : indexPath)
4040 section. rows. append ( row)
4141 }
4242
@@ -45,45 +45,38 @@ class OriginalTable {
4545
4646 insertIndexPaths = Array ( repeating: IndexPath ( ) , count: totalNumberOfRows)
4747 deleteIndexPaths = Array ( repeating: IndexPath ( ) , count: totalNumberOfRows)
48- updateIndexPaths = Array ( repeating: IndexPath ( ) , count: totalNumberOfRows)
48+ reloadIndexPaths = Array ( repeating: IndexPath ( ) , count: totalNumberOfRows)
4949
5050 self . tableView = tableView
5151 self . config = config
5252 }
5353
54- func originalRowWithIndexPath( _ indexPath: IndexPath ) -> OriginalRow {
55- let section = sections [ indexPath. section]
56- let row = section. rows [ indexPath. row]
57-
58- return row
59- }
60-
61- func vissibleOriginalRowWithIndexPath( _ indexPath: IndexPath ) -> OriginalRow {
54+ func vissibleRow( with indexPath: IndexPath ) -> TableRow {
6255 let section = sections [ indexPath. section]
6356 let visibleRows = section. rows. filter { !$0. hiding }
6457
6558 return visibleRows [ indexPath. row]
6659 }
6760
68- func originalRowWithTableViewCell ( _ cell: UITableViewCell ) -> OriginalRow {
61+ func row ( with cell: UITableViewCell ) -> TableRow {
6962 let allRows = sections. flatMap { $0. rows }
7063
7164 return allRows. filter { $0. cell === cell } . first!
7265 }
7366
74- func indexPathForInsertingOriginalRow ( _ originalRow : OriginalRow ) -> IndexPath {
75- let indexSection = originalRow . originalIndexPath . section
76- var indexRow = originalRow . originalIndexPath . row
67+ func insert ( row : TableRow ) -> IndexPath {
68+ let indexSection = row . indexPath . section
69+ var indexRow = row . indexPath . row
7770
7871 let section = sections [ indexSection]
7972 indexRow = section. rows [ 0 ..< indexRow] . filter { !$0. hiding } . count
8073
8174 return IndexPath ( row: indexRow, section: indexSection)
8275 }
8376
84- func indexPathForDeletingOriginalRow ( _ originalRow : OriginalRow ) -> IndexPath {
85- let indexSection = originalRow . originalIndexPath . section
86- var indexRow = originalRow . originalIndexPath . row
77+ func delete ( row : TableRow ) -> IndexPath {
78+ let indexSection = row . indexPath . section
79+ var indexRow = row . indexPath . row
8780
8881 let section = sections [ indexSection]
8982 indexRow = section. rows [ 0 ..< indexRow] . filter { !$0. hidden } . count
@@ -94,20 +87,20 @@ class OriginalTable {
9487 func prepareUpdates( ) {
9588 insertIndexPaths. removeAll ( )
9689 deleteIndexPaths. removeAll ( )
97- updateIndexPaths . removeAll ( )
90+ reloadIndexPaths . removeAll ( )
9891
9992 let allRows = sections. flatMap { $0. rows }
10093
10194 allRows. forEach { ( row) in
10295 if row. batchOperation == . delete {
103- let indexPath = indexPathForDeletingOriginalRow ( row)
96+ let indexPath = delete ( row : row)
10497 deleteIndexPaths. append ( indexPath)
10598 } else if row. batchOperation == . insert {
106- let indexPath = indexPathForInsertingOriginalRow ( row)
99+ let indexPath = insert ( row : row)
107100 insertIndexPaths. append ( indexPath)
108101 } else if row. batchOperation == . update {
109- let indexPath = indexPathForInsertingOriginalRow ( row)
110- updateIndexPaths . append ( indexPath)
102+ let indexPath = insert ( row : row)
103+ reloadIndexPaths . append ( indexPath)
111104 }
112105 }
113106
@@ -132,9 +125,9 @@ class OriginalTable {
132125
133126 tableView. beginUpdates ( )
134127
135- tableView. reloadRows ( at: updateIndexPaths , with: config. reloadTableViewRowAnimation )
136- tableView. insertRows ( at: insertIndexPaths, with: config. insertTableViewRowAnimation )
137- tableView. deleteRows ( at: deleteIndexPaths, with: config. deleteTableViewRowAnimation )
128+ tableView. reloadRows ( at: reloadIndexPaths , with: config. reloadAnimation )
129+ tableView. insertRows ( at: insertIndexPaths, with: config. insertAnimation )
130+ tableView. deleteRows ( at: deleteIndexPaths, with: config. deleteAnimation )
138131
139132 tableView. endUpdates ( )
140133
0 commit comments