Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 14d4df0

Browse files
TableEditingController refinements/fixes, follow-up to jessesquires#100, jessesquires#80
- Only allow passing tableEditingController via init - Restrict init with tableEditingController to extension for tableview-only - Update changelog, close jessesquires#82 - Example cleanup, refinements, formatting
1 parent d58925c commit 14d4df0

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This release closes the [7.0.0 milestone](https://github.com/jessesquires/JSQDat
1818
## New
1919

2020
- Added new `DataSourceProtocol` extension convenience method `func item(atIndexPath indexPath: IndexPath) -> Item?`.
21+
- Added new `TableEditingController` to support table view editing functionality provided by `UITableViewDataSource`. (#29, #80, #100)
2122

2223
6.0.0
2324
-----

‎Example/Sources/FetchedTableViewController.swift‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import CoreData
2121
import ExampleModel
2222
import JSQDataSourcesKit
2323

24-
25-
class FetchedTableViewController: UITableViewController {
24+
final class FetchedTableViewController: UITableViewController {
2625

2726
// MARK: Properties
2827

@@ -61,10 +60,10 @@ class FetchedTableViewController: UITableViewController {
6160

6261
// ** optional editing **
6362
// if needed, enable the editing functionality on the tableView
64-
let tableDataSourceEditingController: TableEditingController<FetchedResultsController<Thing>> = TableEditingController(
63+
let editingController: TableEditingController<FetchedResultsController<Thing>> = TableEditingController(
6564
canEditRow: { (item, tableView, indexPath) -> Bool in
6665
return item?.color == Color.Blue
67-
},
66+
},
6867
commitEditing: { (dataSource: inout FetchedResultsController<Thing>, tableView, editingStyle, indexPath) in
6968
if editingStyle == .delete {
7069
guard let item = dataSource.item(atIndexPath: indexPath) else { return }
@@ -73,7 +72,10 @@ class FetchedTableViewController: UITableViewController {
7372
})
7473

7574
// 5. create data source provider
76-
dataSourceProvider = DataSourceProvider(dataSource: frc, cellFactory: factory, supplementaryFactory: factory, tableEditingController: tableDataSourceEditingController)
75+
dataSourceProvider = DataSourceProvider(dataSource: frc,
76+
cellFactory: factory,
77+
supplementaryFactory: factory,
78+
tableEditingController: editingController)
7779

7880
// 6. set data source
7981
tableView.dataSource = dataSourceProvider?.tableViewDataSource
@@ -101,14 +103,14 @@ class FetchedTableViewController: UITableViewController {
101103
@IBAction func didTapActionButton(_ sender: UIBarButtonItem) {
102104
UIAlertController.showActionAlert(self, addNewAction: {
103105
self.addNewThing()
104-
}, deleteAction: {
105-
self.deleteSelected()
106-
}, changeNameAction: {
107-
self.changeNameSelected()
108-
}, changeColorAction: {
109-
self.changeColorSelected()
110-
}, changeAllAction: {
111-
self.changeAllSelected()
106+
}, deleteAction: {
107+
self.deleteSelected()
108+
}, changeNameAction: {
109+
self.changeNameSelected()
110+
}, changeColorAction: {
111+
self.changeColorSelected()
112+
}, changeAllAction: {
113+
self.changeAllSelected()
112114
})
113115
}
114116

@@ -150,5 +152,4 @@ class FetchedTableViewController: UITableViewController {
150152
stack.saveAndWait()
151153
fetchData()
152154
}
153-
154155
}

‎Example/Sources/TableViewController.swift‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
//
1818

1919
import UIKit
20-
2120
import JSQDataSourcesKit
2221

23-
2422
final class TableViewController: UITableViewController {
2523

2624
typealias TableCellFactory = ViewFactory<CellViewModel, UITableViewCell>
@@ -45,10 +43,10 @@ final class TableViewController: UITableViewController {
4543

4644
// ** optional editing **
4745
// if needed, enable the editing functionality on the tableView
48-
let tableDataSourceEditingController: TableEditingController<DataSource<Section<CellViewModel>>> = TableEditingController(
46+
let editingController: TableEditingController<DataSource<Section<CellViewModel>>> = TableEditingController(
4947
canEditRow: { (item, tableView, indexPath) -> Bool in
5048
return true
51-
},
49+
},
5250
commitEditing: { (dataSource: inout DataSource, tableView, editingStyle, indexPath) in
5351
if editingStyle == .delete {
5452
if let _ = dataSource.remove(at: indexPath) {
@@ -58,10 +56,12 @@ final class TableViewController: UITableViewController {
5856
})
5957

6058
// 3. create data source provider
61-
dataSourceProvider = DataSourceProvider(dataSource: dataSource, cellFactory: factory, supplementaryFactory: factory, tableEditingController: tableDataSourceEditingController)
59+
dataSourceProvider = DataSourceProvider(dataSource: dataSource,
60+
cellFactory: factory,
61+
supplementaryFactory: factory,
62+
tableEditingController: editingController)
6263

6364
// 4. set data source
6465
tableView.dataSource = dataSourceProvider?.tableViewDataSource
65-
6666
}
6767
}

‎Source/DataSourceProvider.swift‎

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,52 @@ where CellFactory.Item == DataSource.Item, SupplementaryFactory.Item == DataSour
3737
public let supplementaryFactory: SupplementaryFactory
3838

3939
fileprivate var bridgedDataSource: BridgedDataSource?
40-
fileprivate var _tableEditingController: TableEditingController<DataSource>?
40+
41+
fileprivate var tableEditingController: TableEditingController<DataSource>?
4142

4243
// MARK: Initialization
4344

44-
/**
45-
Initializes a new data source provider.
46-
47-
- parameter dataSource: The data source.
48-
- parameter cellFactory: The cell factory.
49-
- parameter supplementaryFactory: The supplementary view factory.
50-
- parameter tableEditingController: The tableEditing controller.
51-
52-
- returns: A new `DataSourceProvider` instance.
53-
54-
- warning: Table views do not have supplementary views, but this parameter is still required in order to satisfy
55-
the generic constraints for Swift. You can simply pass the same `cellFactory` here. The parameter will be ignored.
56-
The same applies to collection views that do not have supplementary views. Again, the parameter will be ignored.
57-
*/
58-
public init(dataSource: DataSource, cellFactory: CellFactory, supplementaryFactory: SupplementaryFactory, tableEditingController: TableEditingController<DataSource>? = nil) {
45+
46+
/// Initializes a new data source provider.
47+
///
48+
/// - Parameters:
49+
/// - dataSource: The data source.
50+
/// - cellFactory: The cell factory.
51+
/// - supplementaryFactory: The supplementary view factory.
52+
///
53+
/// - Warning: Table views do not have supplementary views, but this parameter is still required in order to satisfy
54+
/// the generic constraints for Swift. You can simply pass the same `cellFactory` here. The parameter will be ignored.
55+
/// The same applies to collection views that do not have supplementary views. Again, the parameter will be ignored.
56+
public init(dataSource: DataSource,
57+
cellFactory: CellFactory,
58+
supplementaryFactory: SupplementaryFactory) {
5959
self.dataSource = dataSource
6060
self.cellFactory = cellFactory
6161
self.supplementaryFactory = supplementaryFactory
62-
self._tableEditingController = tableEditingController
6362
}
6463
}
6564

6665
public extension DataSourceProvider where CellFactory.View: UITableViewCell {
67-
66+
67+
/// Initializes a new data source provider.
68+
///
69+
/// - Parameters:
70+
/// - dataSource: The data source.
71+
/// - cellFactory: The cell factory.
72+
/// - supplementaryFactory: The supplementary view factory.
73+
/// - tableEditingController: The table editing controller.
74+
///
75+
/// - Warning: Table views do not have supplementary views, but this parameter is still required in order to satisfy
76+
/// the generic constraints for Swift. You can simply pass the same `cellFactory` here. The parameter will be ignored.
77+
/// The same applies to collection views that do not have supplementary views. Again, the parameter will be ignored.
78+
public convenience init(dataSource: DataSource,
79+
cellFactory: CellFactory,
80+
supplementaryFactory: SupplementaryFactory,
81+
tableEditingController: TableEditingController<DataSource>? = nil) {
82+
self.init(dataSource: dataSource, cellFactory: cellFactory, supplementaryFactory: supplementaryFactory)
83+
self.tableEditingController = tableEditingController
84+
}
85+
6886
// MARK: UITableViewDataSource
6987

7088
/// Returns the `UITableViewDataSource` object.
@@ -75,16 +93,6 @@ public extension DataSourceProvider where CellFactory.View: UITableViewCell {
7593
return bridgedDataSource!
7694
}
7795

78-
/// The table editing controller for this data source provider.
79-
public var tableEditingController: TableEditingController<DataSource>? {
80-
set {
81-
_tableEditingController = newValue
82-
}
83-
get {
84-
return _tableEditingController
85-
}
86-
}
87-
8896
private func tableViewBridgedDataSource() -> BridgedDataSource {
8997
let dataSource = BridgedDataSource(
9098
numberOfSections: { [unowned self] () -> Int in

‎Tests/DataSourceProviderTests.swift‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ final class DataSourceProviderTests: TestCase {
399399
}
400400

401401
// GIVEN: a data source editing controller
402-
let tableDataSourceEditingController = TableEditingController<DataSource<Section<FakeViewModel>>>(
402+
let editingController = TableEditingController<DataSource<Section<FakeViewModel>>>(
403403
canEditRow: { (item, tableView, indexPath) -> Bool in
404404
return indexPath == expectedIndexPath
405405
},
@@ -412,8 +412,10 @@ final class DataSourceProviderTests: TestCase {
412412
})
413413

414414
// GIVEN: a data source provider
415-
dataSourceProvider = DataSourceProvider(dataSource: dataSource, cellFactory: factory, supplementaryFactory: factory)
416-
dataSourceProvider.tableEditingController = tableDataSourceEditingController
415+
dataSourceProvider = DataSourceProvider(dataSource: dataSource,
416+
cellFactory: factory,
417+
supplementaryFactory: factory,
418+
tableEditingController: editingController)
417419

418420
let tableViewDataSource = dataSourceProvider.tableViewDataSource
419421
tableView.dataSource = tableViewDataSource

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /