-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathImmuTableDiffableViewHandler.swift
45 lines (38 loc) · 1.87 KB
/
ImmuTableDiffableViewHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import UIKit
public class ImmuTableDiffableViewHandler: ImmuTableViewHandler {
public lazy var diffableDataSource: ImmuTableDiffableDataSource = {
return ImmuTableDiffableDataSource(tableView: target.tableView) { tableView, indexPath, item in
let row = item.immuTableRow
let cell = tableView.dequeueReusableCell(withIdentifier: row.reusableIdentifier, for: indexPath)
row.configureCell(cell)
return cell
}
}()
public override init(takeOver target: UIViewControllerWithTableView, with passthroughScrollViewDelegate: UIScrollViewDelegate? = nil) {
super.init(takeOver: target, with: passthroughScrollViewDelegate)
self.target.tableView.dataSource = diffableDataSource
self.automaticallyReloadTableView = false
}
func item(for indexPath: IndexPath) -> ImmuTableRow? {
guard let diffableDataSource = target.tableView.dataSource as? UITableViewDiffableDataSource<AnyHashable, AnyHashableImmuTableRow> else {
return nil
}
return diffableDataSource.itemIdentifier(for: indexPath)?.immuTableRow
}
open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if target.responds(to: #selector(UITableViewDelegate.tableView(_:didSelectRowAt:))) {
target.tableView?(tableView, didSelectRowAt: indexPath)
} else if let item = item(for: indexPath) {
item.action?(item)
}
if automaticallyDeselectCells {
tableView.deselectRow(at: indexPath, animated: true)
}
}
open override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let item = item(for: indexPath), let customHeight = type(of: item).customHeight {
return CGFloat(customHeight)
}
return tableView.rowHeight
}
}