Skip to content

Commit efc0dcd

Browse files
Add table view support
1 parent 79e5d15 commit efc0dcd

File tree

10 files changed

+361
-31
lines changed

10 files changed

+361
-31
lines changed

DataProvider.swift

Lines changed: 143 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,79 +12,159 @@ open class DataProvider<Itm: Item>: DataProviderWrapper {
1212
public override init() {
1313
}
1414

15-
override public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
15+
private func numberOfItemsInSection(section: Int) -> Int {
1616
return fastAdapter?.adapter?.itemList[section].items.count ?? 0
1717
}
1818

19-
public override func numberOfSections(in collectionView: UICollectionView) -> Int {
19+
private func numberOfSections() -> Int {
2020
return fastAdapter?.adapter?.itemList.sections.count ?? 0
2121
}
2222

23-
override public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
23+
private func cellForItemAt(listView: ListView, indexPath: IndexPath) -> ListViewCell? {
2424
if let item = fastAdapter?.adapter?.itemList[indexPath.section].items[indexPath.row] {
25-
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: item.getType(), for: indexPath)
25+
var cell = listView.dequeueReusableListViewCell(withReuseIdentifier: item.getType(), for: indexPath)
2626
item.onBind(indexPath: indexPath, cell: &cell)
2727
return cell
2828
}
2929
// Last resort, should never happen
3030
if let firstType = fastAdapter?.typeInstanceCache.typeInstances.first {
31-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: firstType.key, for: indexPath)
31+
let cell = listView.dequeueReusableListViewCell(withReuseIdentifier: firstType.key, for: indexPath)
3232
return cell
3333
}
34-
// Will crash when this happens
35-
return super.collectionView(collectionView, cellForItemAt: indexPath)
34+
return nil
3635
}
3736

38-
public override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
37+
private func viewForSupplementaryElement(listView: ListView, ofKind kind: String, at indexPath: IndexPath) -> (UIView & ListViewReusableView)? {
3938
switch kind {
4039
case UICollectionElementKindSectionHeader:
4140
if let item = fastAdapter?.adapter?.itemList[indexPath.section].header {
42-
var view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: item.getType(), for: indexPath)
41+
guard var view = listView.dequeueReusableListViewSupplementaryView(ofKind: kind, withReuseIdentifier: item.getType(), for: indexPath) else {
42+
return nil
43+
}
4344
item.onBind(indexPath: indexPath, view: &view)
4445
return view
4546
}
4647
case UICollectionElementKindSectionFooter:
4748
if let item = fastAdapter?.adapter?.itemList[indexPath.section].footer {
48-
var view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: item.getType(), for: indexPath)
49+
guard var view = listView.dequeueReusableListViewSupplementaryView(ofKind: kind, withReuseIdentifier: item.getType(), for: indexPath) else {
50+
return nil
51+
}
4952
item.onBind(indexPath: indexPath, view: &view)
5053
return view
5154
}
5255
default:
5356
if let item = fastAdapter?.adapter?.itemList[indexPath.section].supplementaryItems?[kind] {
54-
var view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: item.getType(), for: indexPath)
57+
guard var view = listView.dequeueReusableListViewSupplementaryView(ofKind: kind, withReuseIdentifier: item.getType(), for: indexPath) else {
58+
return nil
59+
}
5560
item.onBind(indexPath: indexPath, view: &view)
5661
return view
5762
}
5863
assertionFailure("unknown supplementary view kind \(kind)")
5964
}
6065
// Last resort, should never happen
6166
if let firstType = fastAdapter?.typeInstanceCache.supplementaryViewTypeInstances[kind]?.first {
62-
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: firstType.key, for: indexPath)
67+
let view = listView.dequeueReusableListViewSupplementaryView(ofKind: kind, withReuseIdentifier: firstType.key, for: indexPath)
6368
return view
6469
}
65-
// Will crash when this happens
66-
return super.collectionView(collectionView, viewForSupplementaryElementOfKind: kind, at: indexPath)
70+
return nil
6771
}
6872

69-
override public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
73+
private func sizeForItem(at indexPath: IndexPath) -> CGSize {
7074
return fastAdapter?.adapter?.itemList[indexPath.section].items[indexPath.row].getSize() ?? .zero
7175
}
7276

73-
open override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
77+
private func sizeForHeader(in section: Int) -> CGSize {
7478
return fastAdapter?.adapter?.itemList[section].header?.getSize() ?? .zero
7579
}
7680

77-
public override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
81+
private func sizeForFooter(in section: Int) -> CGSize {
7882
return fastAdapter?.adapter?.itemList[section].footer?.getSize() ?? .zero
7983
}
8084

81-
public override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
85+
private func canMoveItem(at indexPath: IndexPath) -> Bool {
8286
return (fastAdapter?.adapter?.itemList[indexPath.section].items[indexPath.row] as? Draggable)?.isDraggable ?? false
8387
}
8488

85-
public override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
89+
private func move(moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
8690
fastAdapter?.adapter?.itemList.move(source: sourceIndexPath, destination: destinationIndexPath)
8791
}
92+
93+
override public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
94+
return numberOfItemsInSection(section: section)
95+
}
96+
97+
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
98+
return numberOfItemsInSection(section: section)
99+
}
100+
101+
public override func numberOfSections(in collectionView: UICollectionView) -> Int {
102+
return numberOfSections()
103+
}
104+
105+
public override func numberOfSections(in tableView: UITableView) -> Int {
106+
return numberOfSections()
107+
}
108+
109+
override public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
110+
return cellForItemAt(listView: collectionView, indexPath: indexPath) as! UICollectionViewCell? ?? super.collectionView(collectionView, cellForItemAt: indexPath)
111+
}
112+
113+
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
114+
return cellForItemAt(listView: tableView, indexPath: indexPath) as! UITableViewCell? ?? super.tableView(tableView, cellForRowAt: indexPath)
115+
}
116+
117+
public override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
118+
return viewForSupplementaryElement(listView: collectionView, ofKind: kind, at: indexPath) as! UICollectionReusableView? ?? super.collectionView(collectionView, viewForSupplementaryElementOfKind: kind, at: indexPath)
119+
}
120+
121+
public override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
122+
return viewForSupplementaryElement(listView: tableView, ofKind: UICollectionElementKindSectionHeader, at: IndexPath(row: 0, section: section))
123+
}
124+
125+
public override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
126+
return viewForSupplementaryElement(listView: tableView, ofKind: UICollectionElementKindSectionFooter, at: IndexPath(row: 0, section: section))
127+
}
128+
129+
override public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
130+
return sizeForItem(at: indexPath)
131+
}
132+
133+
public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
134+
return sizeForItem(at: indexPath).height
135+
}
136+
137+
open override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
138+
return sizeForHeader(in: section)
139+
}
140+
141+
public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
142+
return sizeForHeader(in: section).height
143+
}
144+
145+
public override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
146+
return sizeForFooter(in: section)
147+
}
148+
149+
public override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
150+
return sizeForFooter(in: section).height
151+
}
152+
153+
public override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
154+
return canMoveItem(at: indexPath)
155+
}
156+
157+
public override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
158+
return canMoveItem(at: indexPath)
159+
}
160+
161+
public override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
162+
move(moveItemAt: sourceIndexPath, to: destinationIndexPath)
163+
}
164+
165+
public override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
166+
move(moveItemAt: sourceIndexPath, to: destinationIndexPath)
167+
}
88168
}
89169

90170
open class DataProviderWrapper: NSObject {
@@ -134,3 +214,47 @@ extension DataProviderWrapper: UICollectionViewDelegateFlowLayout {
134214

135215
}
136216
}
217+
218+
extension DataProviderWrapper: UITableViewDataSource {
219+
public func numberOfSections(in tableView: UITableView) -> Int {
220+
return 0
221+
}
222+
223+
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
224+
return 0
225+
}
226+
227+
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
228+
return UITableViewCell()
229+
}
230+
231+
public func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
232+
return false
233+
}
234+
235+
public func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
236+
237+
}
238+
}
239+
240+
extension DataProviderWrapper: UITableViewDelegate {
241+
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
242+
return 0
243+
}
244+
245+
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
246+
return 0
247+
}
248+
249+
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
250+
return 0
251+
}
252+
253+
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
254+
return nil
255+
}
256+
257+
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
258+
return nil
259+
}
260+
}

FastAdapter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class FastAdapter<Itm: Item> {
2929
public var dataProvider: DataProvider<Itm> {
3030
didSet {
3131
dataProvider.fastAdapter = self
32-
listView?.dataSource = dataProvider
33-
listView?.delegate = dataProvider
32+
listView?.setListViewDelegate(delegate: dataProvider)
33+
listView?.setListViewDataSource(dataSource: dataProvider)
3434
}
3535
}
3636
public var typeInstanceCache: TypeInstanceCache<Itm> {
@@ -48,7 +48,7 @@ public class FastAdapter<Itm: Item> {
4848
notifier.fastAdapter = self
4949
}
5050
}
51-
var listView: UICollectionView? {
51+
public var listView: ListView? {
5252
didSet {
5353
typeInstanceCache.renew()
5454
}

0 commit comments

Comments
 (0)