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)
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)
let updates = TableViewDiffableSection(id: "SectionID here")
updates.addHeader(header)
  • Call updates on the source
source.update(sections: [updates])