DiffableTableViewDataSource
public final class DiffableTableViewDataSource : DiffableDataSource
extension DiffableTableViewDataSource: UITableViewDataSource
extension DiffableTableViewDataSource: UITableViewDelegate
The core of EZSource
for UITableView
. Provides declarative API to work with UITableView
All you need is to initialize the source, create rows, add them to section updates and call update function
The rest is handled by EZSource
, next time you have to update the UITableView
, EZSource
will find take care about inserting, deleting or reloading rows using animation provided in the section updates
Usage Example
- Define a Cell Model
struct StringCellModel: Hashable {
let uniqueID: String
let text: String
// Defines uniqueness of the model
func hash(into hasher: inout Hasher) {
hasher.combine(uniqueID)
}
// Defines dynamic context of the model
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.text == rhs.text
}
}
- Define a Cell
final class StringCell: UITableViewCell, ReusableCell, Configurable {
typealias Model = StringCellModel
func configure(with model: StringCellModel) {
textLabel?.text = "ID: \(model.uniqueID): \(model.text)"
selectionStyle = .none
}
}
- Create Data Source
let config = DiffableTableViewDataSource.Config(tableView: tableView,
cellTypes: [StringCell.self],
headerFooters: [TestReusableView.self])
source = DiffableTableViewDataSource(config: config)
- create a
TableViewRow
let model = StringCellModel(uniqueID: ID, text: title)
let row = TableViewRow<StringCell>(model: model,
onTap: { debugPrint("tapped with \($0)")})
// add rows if need
row.addRowLeadingActions(leadingActions)
row.addRowTrailingActions(trailingActions)
- Add the row to the cell
let updates = TableViewDiffableSection(id: "SectionID here")
updates.addRows([row])
- Create a ReusableView
final class TestReusableView: ReusableView, Configurable {
let label: UILabel
override init(reuseIdentifier: String?) {
// Init and Configure UILabel
super.init(reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func configure(with txt: String) {
label.text = txt
}
}
- Create a header
let text = "My custom section"
let footer = ImmutableHeaderFooterProvider<TestReusableView>(model: text)
- Attach the header to a
TableViewDiffableSection
let updates = TableViewDiffableSection(id: "SectionID here")
updates.addHeader(header)
- Call updates on the source
source.update(sections: [updates])
-
Launch update on the Data source will find animate updates depending on the sections settings
Declaration
Swift
public func update(sections: [TableViewDiffableSection])
Parameters
sections
-
Expands or Collapses the section with id depending on the current state of the section
Declaration
Swift
public func autoCollapseExpandSection(with id: String)
Parameters
id
String
-
Undocumented
Declaration
Swift
public func numberOfSections(in tableView: UITableView) -> Int
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
-
Undocumented
Declaration
Swift
public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
-
Config to initialize
DiffableTableViewDataSource
The required info isUITableView
and [ReusableCell
.Type] [ReusableView
.Type] andDiffableDataSourceFilterProvider
are optionalNote
Though you don’t have to passDiffableDataSourceFilterProvider
the source won’t know about the number of sections, so it will create sections by itsefl bases on the first updateDeclaration
Swift
public struct Config