@@ -95,22 +95,28 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
9595 tableView? . reloadData ( )
9696 }
9797
98+ // MARK: - Private
99+ private func row( at indexPath: IndexPath ) -> Row ? {
100+ if indexPath. section < sections. count && indexPath. row < sections [ indexPath. section] . rows. count {
101+ return sections [ indexPath. section] . rows [ indexPath. row]
102+ }
103+ return nil
104+ }
105+
98106 // MARK: Public
99107 @discardableResult
100108 open func invoke(
101109 action: TableRowActionType ,
102110 cell: UITableViewCell ? , indexPath: IndexPath ,
103111 userInfo: [ AnyHashable : Any ] ? = nil ) -> Any ?
104112 {
105- if indexPath. section < sections. count && indexPath. row < sections [ indexPath. section] . rows. count {
106- return sections [ indexPath. section] . rows [ indexPath. row] . invoke (
107- action: action,
108- cell: cell,
109- path: indexPath,
110- userInfo: userInfo
111- )
112- }
113- return nil
113+ guard let row = row ( at: indexPath) else { return nil }
114+ return row. invoke (
115+ action: action,
116+ cell: cell,
117+ path: indexPath,
118+ userInfo: userInfo
119+ )
114120 }
115121
116122 open override func responds( to selector: Selector ) -> Bool {
@@ -125,7 +131,8 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
125131
126132 // MARK: - Internal
127133 func hasAction( _ action: TableRowActionType , atIndexPath indexPath: IndexPath ) -> Bool {
128- return sections [ indexPath. section] . rows [ indexPath. row] . has ( action: action)
134+ guard let row = row ( at: indexPath) else { return false }
135+ return row. has ( action: action)
129136 }
130137
131138 @objc
@@ -172,6 +179,8 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
172179 }
173180
174181 open func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
182+ guard section < sections. count else { return 0 }
183+
175184 return sections [ section] . numberOfRows
176185 }
177186
@@ -196,29 +205,39 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
196205
197206 // MARK: UITableViewDataSource - section setup
198207 open func tableView( _ tableView: UITableView , titleForHeaderInSection section: Int ) -> String ? {
208+ guard section < sections. count else { return nil }
209+
199210 return sections [ section] . headerTitle
200211 }
201212
202213 open func tableView( _ tableView: UITableView , titleForFooterInSection section: Int ) -> String ? {
214+ guard section < sections. count else { return nil }
215+
203216 return sections [ section] . footerTitle
204217 }
205218
206219 // MARK: UITableViewDelegate - section setup
207220 open func tableView( _ tableView: UITableView , viewForHeaderInSection section: Int ) -> UIView ? {
221+ guard section < sections. count else { return nil }
222+
208223 return sections [ section] . headerView
209224 }
210225
211226 open func tableView( _ tableView: UITableView , viewForFooterInSection section: Int ) -> UIView ? {
227+ guard section < sections. count else { return nil }
228+
212229 return sections [ section] . footerView
213230 }
214231
215232 open func tableView( _ tableView: UITableView , heightForHeaderInSection section: Int ) -> CGFloat {
233+ guard section < sections. count else { return 0 }
216234
217235 let section = sections [ section]
218236 return section. headerHeight ?? section. headerView? . frame. size. height ?? UITableView . automaticDimension
219237 }
220238
221239 open func tableView( _ tableView: UITableView , heightForFooterInSection section: Int ) -> CGFloat {
240+ guard section < sections. count else { return 0 }
222241
223242 let section = sections [ section]
224243 return section. footerHeight
0 commit comments