This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ReusableTableViewCell: UITableViewCell { | |
static let identifier = String(describing: ReusableTableViewCell.self) | |
var leading: UIView? { | |
didSet { | |
if let oldValue = oldValue { | |
stackView.removeArrangedSubview(oldValue) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UITableView { | |
static private let snapshotKey = "snapshot" | |
var descriptor: UITableViewDescriptor? { | |
get { objc_getAssociatedObject(self, Self.snapshotKey) as? UITableViewDescriptor } | |
set { | |
objc_setAssociatedObject(self, Self.snapshotKey, newValue, .OBJC_ASSOCIATION_ASSIGN) | |
delegate = newValue | |
newValue?.tableView = self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tableView.descriptor = UITableViewDescriptor(sections: [ | |
UITableViewSection( | |
data: $data, // data: [Int], $data: Publisher<Int, Never> | |
cellProvider: {(tableView, indexPath, element) in | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell | |
cell.titleLabel.text = "Item #\(element)" | |
return cell | |
} | |
) | |
]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol IdentifiableCell { | |
static var identifier: String { get } | |
} | |
extension IdentifiableCell { | |
static var identifier: String { String(describing: Self.self) } | |
} | |
extension IdentifiableCell where Self: UITableViewCell { | |
static func dequeue(in tableView: UITableView) -> Self { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UITableViewDescriptor: NSObject { | |
var tableView: UITableView? { | |
didSet { | |
configureBindings() | |
} | |
} | |
private var dataSource: UITableViewDiffableDataSource<Int, AnyHashable>! | |
private var sections = [UITableViewSection]() | |
private let sectionsPublisher: AnyPublisher<[UITableViewSection], Never> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias SectionType: Hashable // The type you're using to identify sections. | |
typealias DataType: Hashable // The type of the data you're displaying. | |
let dataSource = UITableViewDiffableDataSource<SectionType, DataType>( | |
tableView: tableView, | |
cellProvider: {(tableView, indexPath, data) in | |
// construct and return a cell here | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UITableViewSection: NSObject { | |
private(set) var items = [AnyHashable]() | |
private(set) var sectionIndex: Int! | |
private let itemsPublisher: AnyPublisher<[AnyHashable], Never> | |
private let cellProvider: (UITableView, IndexPath, AnyHashable) -> UITableViewCell | |
private var subscriptions = Set<AnyCancellable>() | |
convenience init<T: Hashable>( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tableView.descriptor = UITableViewDescriptor(sections: [ | |
UITableViewSection( | |
items: $data, | |
cellProvider: {(tableView, indexPath, element) in | |
let cell = Cell.dequeue(in: tableView) | |
cell.configure(with: element) | |
return cell | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dataSource = DataSource() | |
dataSource.sections = [ | |
Section(rows: [ | |
Row(text: "Hello") | |
]) | |
] | |
dataSource.tableView = tableView |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UITableViewCoordinator: UITableViewDataSource, UITableViewDelegate { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
data.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier") as! Cell | |
// configure it | |
return cell | |
} |
NewerOlder