Skip to content

Commit baea234

Browse files
committed
Added delegate as wrapper optionals data source methods
1 parent 903da59 commit baea234

5 files changed

Lines changed: 54 additions & 3 deletions

File tree

SPDiffable.xcodeproj/project.pbxproj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
F4632035249EBA7100AF7413 /* SPTableDiffableDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4632034249EBA7100AF7413 /* SPTableDiffableDelegate.swift */; };
1011
F4954BE2249B788A007072FA /* SPDiffable.h in Headers */ = {isa = PBXBuildFile; fileRef = F4954BE0249B788A007072FA /* SPDiffable.h */; settings = {ATTRIBUTES = (Public, ); }; };
1112
F4954BEE249B95F2007072FA /* FUNDING.yml in Resources */ = {isa = PBXBuildFile; fileRef = F4954BED249B95F2007072FA /* FUNDING.yml */; };
1213
F4954BF7249B962C007072FA /* Readme.md in Resources */ = {isa = PBXBuildFile; fileRef = F4954BF2249B962C007072FA /* Readme.md */; };
@@ -21,6 +22,7 @@
2122
/* End PBXBuildFile section */
2223

2324
/* Begin PBXFileReference section */
25+
F4632034249EBA7100AF7413 /* SPTableDiffableDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPTableDiffableDelegate.swift; sourceTree = "<group>"; };
2426
F4954BDD249B788A007072FA /* SPDiffable.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SPDiffable.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2527
F4954BE0249B788A007072FA /* SPDiffable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPDiffable.h; sourceTree = "<group>"; };
2628
F4954BE1249B788A007072FA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -50,6 +52,15 @@
5052
/* End PBXFrameworksBuildPhase section */
5153

5254
/* Begin PBXGroup section */
55+
F4632036249EBB9200AF7413 /* Views */ = {
56+
isa = PBXGroup;
57+
children = (
58+
F4954C01249B9715007072FA /* SPDiffableTableController.swift */,
59+
F4954BFF249B9715007072FA /* SPDiffableTableView.swift */,
60+
);
61+
path = Views;
62+
sourceTree = "<group>";
63+
};
5364
F4954BD3249B788A007072FA = {
5465
isa = PBXGroup;
5566
children = (
@@ -129,9 +140,9 @@
129140
F4954BFE249B9715007072FA /* Table */ = {
130141
isa = PBXGroup;
131142
children = (
132-
F4954BFF249B9715007072FA /* SPDiffableTableView.swift */,
143+
F4632036249EBB9200AF7413 /* Views */,
133144
F4954C00249B9715007072FA /* SPTableDiffableDataSource.swift */,
134-
F4954C01249B9715007072FA /* SPDiffableTableController.swift */,
145+
F4632034249EBA7100AF7413 /* SPTableDiffableDelegate.swift */,
135146
);
136147
path = Table;
137148
sourceTree = "<group>";
@@ -225,6 +236,7 @@
225236
F4954C03249B9716007072FA /* SPDiffableFooter.swift in Sources */,
226237
F4954C06249B9716007072FA /* SPDiffableSection.swift in Sources */,
227238
F4954C07249B9716007072FA /* SPDiffableTableView.swift in Sources */,
239+
F4632035249EBA7100AF7413 /* SPTableDiffableDelegate.swift in Sources */,
228240
F4954C08249B9716007072FA /* SPTableDiffableDataSource.swift in Sources */,
229241
);
230242
runOnlyForDeploymentPostprocessing = 0;

Source/SPDiffable/Table/SPTableDiffableDataSource.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import UIKit
2323

2424
open class SPTableDiffableDataSource: UITableViewDiffableDataSource<SPDiffableSection, SPDiffableItem> {
2525

26+
public weak var diffableDelegate: SPTableDiffableDelegate?
27+
2628
public init(tableView: UITableView, cellProviders: [CellProvider]) {
2729
super.init(tableView: tableView, cellProvider: { (tableView, indexPath, model) -> UITableViewCell? in
2830
for provider in cellProviders {
@@ -45,21 +47,27 @@ open class SPTableDiffableDataSource: UITableViewDiffableDataSource<SPDiffableSe
4547
}
4648

4749
public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
50+
if let title = diffableDelegate?.tableView(tableView, titleForHeaderInSection: section) {
51+
return title
52+
}
4853
if let header = snapshot().sectionIdentifiers[section].header as? SPDiffableTextHeader {
4954
return header.text
5055
}
5156
return nil
5257
}
5358

5459
public override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
60+
if let title = diffableDelegate?.tableView(tableView, titleForFooterInSection: section) {
61+
return title
62+
}
5563
if let footer = snapshot().sectionIdentifiers[section].footer as? SPDiffableTextFooter {
5664
return footer.text
5765
}
5866
return nil
5967
}
6068

6169
public override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
62-
return true
70+
return diffableDelegate?.tableView(tableView, canEditRowAt: indexPath) ?? false
6371
}
6472
}
6573

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2020 Ivan Varabei (varabeis@icloud.com)
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
24+
public protocol SPTableDiffableDelegate: class {
25+
26+
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
27+
28+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
29+
30+
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?
31+
}

Source/SPDiffable/Table/SPDiffableTableController.swift renamed to Source/SPDiffable/Table/Views/SPDiffableTableController.swift

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)