1- //
2- // DataSource+UITableView.swift
3- // DataSource
4- //
5- // Created by Matthias Buchetics on 21/02/2017.
6- // Copyright © 2017 aaa - all about apps GmbH. All rights reserved.
7- //
8-
9- import UIKit
101import Differ
2+ import UIKit
113
124extension DataSource : UITableViewDataSource {
13-
5+
146 // MARK: Counts
15-
16- public func numberOfSections( in tableView : UITableView ) -> Int {
7+
8+ public func numberOfSections( in _ : UITableView ) -> Int {
179 return visibleSections. count
1810 }
19-
20- public func tableView( _ tableView : UITableView , numberOfRowsInSection section: Int ) -> Int {
11+
12+ public func tableView( _: UITableView , numberOfRowsInSection section: Int ) -> Int {
2113 return visibleSections [ section] . numberOfVisibleRows
2214 }
23-
15+
2416 // MARK: Configuration
25-
17+
2618 public func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
27- let cellDescriptor = self . cellDescriptor ( at: indexPath)
19+ guard let cellDescriptor = cellDescriptor ( at: indexPath) else {
20+ fatalError ( " [DataSource] no cellDescriptor found for indexPath \( indexPath) with identifier \( visibleRow ( at: indexPath) ? . identifier) " )
21+ }
22+
2823 let cellIdentifier = cellDescriptor. cellIdentifier
2924 let bundle = cellDescriptor. bundle ?? Bundle . main
30-
31- if registerNibs && !reuseIdentifiers. contains ( cellIdentifier) {
25+
26+ if registerNibs, !reuseIdentifiers. contains ( cellIdentifier) {
3227 if bundle. path ( forResource: cellIdentifier, ofType: " nib " ) != nil {
3328 tableView. registerNib ( cellIdentifier, bundle: bundle)
3429 reuseIdentifiers. insert ( cellIdentifier)
@@ -37,13 +32,15 @@ extension DataSource: UITableViewDataSource {
3732 reuseIdentifiers. insert ( cellIdentifier)
3833 }
3934 }
40-
35+
4136 if let closure = cellDescriptor. configureClosure ?? configure {
42- let row = self . visibleRow ( at: indexPath)
37+ guard let row = visibleRow ( at: indexPath) else {
38+ fatalError ( " [DataSource] no visible row found for indexPath \( indexPath) " )
39+ }
4340 let cell = tableView. dequeueReusableCell ( withIdentifier: cellIdentifier, for: indexPath)
44-
41+
4542 closure ( row, cell, indexPath)
46-
43+
4744 return cell
4845 } else if let fallbackDataSource = fallbackDataSource {
4946 return fallbackDataSource. tableView ( tableView, cellForRowAt: indexPath)
@@ -53,101 +50,125 @@ extension DataSource: UITableViewDataSource {
5350 }
5451
5552 // MARK: Header & Footer
56-
57- public func tableView( _ tableView : UITableView , titleForHeaderInSection section: Int ) -> String ? {
53+
54+ public func tableView( _: UITableView , titleForHeaderInSection section: Int ) -> String ? {
5855 let sectionDescriptor = self . sectionDescriptor ( at: section)
59-
56+
6057 if let closure = sectionDescriptor? . headerClosure ?? sectionHeader {
61- let header = closure ( visibleSection ( at: section) , section)
62-
58+ guard let visibleSection = visibleSection ( at: section) else {
59+ return nil
60+ }
61+
62+ let header = closure ( visibleSection, section)
63+
6364 switch header {
6465 case . title( let title) :
6566 return title
6667 default :
6768 return nil
6869 }
6970 }
70-
71+
7172 return nil
7273 }
73-
74- public func tableView( _ tableView : UITableView , titleForFooterInSection section: Int ) -> String ? {
74+
75+ public func tableView( _: UITableView , titleForFooterInSection section: Int ) -> String ? {
7576 let sectionDescriptor = self . sectionDescriptor ( at: section)
76-
77+
7778 if let closure = sectionDescriptor? . footerClosure ?? sectionFooter {
78- let footer = closure ( visibleSection ( at: section) , section)
79-
79+ guard let visibleSection = visibleSection ( at: section) else {
80+ return nil
81+ }
82+
83+ let footer = closure ( visibleSection, section)
84+
8085 switch footer {
8186 case . title( let title) :
8287 return title
8388 default :
8489 return nil
8590 }
8691 }
87-
92+
8893 return nil
8994 }
90-
95+
9196 // MARK: Editing
92-
97+
9398 public func tableView( _ tableView: UITableView , canEditRowAt indexPath: IndexPath ) -> Bool {
9499 let cellDescriptor = self . cellDescriptor ( at: indexPath)
95-
96- if let closure = cellDescriptor . canEditClosure ?? canEdit {
97- return closure ( visibleRow ( at : indexPath ) , indexPath )
100+
101+ guard let visibleRow = visibleRow ( at : indexPath ) else {
102+ return false
98103 }
99-
104+
105+ if let closure = cellDescriptor? . canEditClosure ?? canEdit {
106+ return closure ( visibleRow, indexPath)
107+ }
108+
100109 return fallbackDataSource? . tableView ? ( tableView, canEditRowAt: indexPath)
101110 ?? false
102111 }
103-
112+
104113 // MARK: Moving & Reordering
105-
114+
106115 public func tableView( _ tableView: UITableView , canMoveRowAt indexPath: IndexPath ) -> Bool {
107116 let cellDescriptor = self . cellDescriptor ( at: indexPath)
108-
109- if let closure = cellDescriptor. canMoveClosure ?? canMove {
110- return closure ( visibleRow ( at: indexPath) , indexPath)
117+
118+ if let closure = cellDescriptor? . canMoveClosure ?? canMove {
119+ guard let visibleRow = visibleRow ( at: indexPath) else {
120+ return false
121+ }
122+
123+ return closure ( visibleRow, indexPath)
111124 }
112-
125+
113126 return fallbackDataSource? . tableView ? ( tableView, canMoveRowAt: indexPath)
114127 ?? false
115128 }
116-
129+
117130 // MARK: Index
118-
131+
119132 public func sectionIndexTitles( for tableView: UITableView ) -> [ String ] ? {
120133 return sectionIndexTitles ? ( )
121134 ?? fallbackDataSource? . sectionIndexTitles ? ( for: tableView)
122135 }
123-
136+
124137 public func tableView( _ tableView: UITableView , sectionForSectionIndexTitle title: String , at index: Int ) -> Int {
125138 return sectionForSectionIndex ? ( title, index)
126139 ?? fallbackDataSource? . tableView ? ( tableView, sectionForSectionIndexTitle: title, at: index)
127140 ?? index
128141 }
129-
142+
130143 // MARK: Data manipulation
131-
144+
132145 public func tableView( _ tableView: UITableView , commit editingStyle: UITableViewCell . EditingStyle , forRowAt indexPath: IndexPath ) {
133146 let cellDescriptor = self . cellDescriptor ( at: indexPath)
134-
135- if let closure = cellDescriptor. commitEditingClosure ?? commitEditing {
136- closure ( visibleRow ( at: indexPath) , editingStyle, indexPath)
147+
148+ if let closure = cellDescriptor? . commitEditingClosure ?? commitEditing {
149+ guard let visibleRow = visibleRow ( at: indexPath) else {
150+ return
151+ }
152+
153+ closure ( visibleRow, editingStyle, indexPath)
137154 return
138155 }
139-
156+
140157 fallbackDataSource? . tableView ? ( tableView, commit: editingStyle, forRowAt: indexPath)
141158 }
142-
159+
143160 public func tableView( _ tableView: UITableView , moveRowAt sourceIndexPath: IndexPath , to destinationIndexPath: IndexPath ) {
144161 let cellDescriptor = self . cellDescriptor ( at: sourceIndexPath)
145-
146- if let closure = cellDescriptor. moveRowClosure ?? moveRow {
147- closure ( visibleRow ( at: sourceIndexPath) , ( sourceIndexPath, destinationIndexPath) )
162+
163+ if let closure = cellDescriptor? . moveRowClosure ?? moveRow {
164+ guard let visibleRow = visibleRow ( at: sourceIndexPath) else {
165+ return
166+ }
167+
168+ closure ( visibleRow, ( sourceIndexPath, destinationIndexPath) )
148169 return
149170 }
150-
171+
151172 fallbackDataSource? . tableView ? ( tableView, moveRowAt: sourceIndexPath, to: destinationIndexPath)
152173 }
153174}
0 commit comments