TableViewAdapter

open class TableViewAdapter<O> : BaseAdapter<O, DefaultTableViewTarget<UITableView>>, UITableViewDataSource where O : DynamicObject

The DiffableDataSource.TableViewAdapterAdapter serves as a UITableViewDataSource that handles ListPublisher snapshots for a UITableView. Subclasses of DiffableDataSource.TableViewAdapter may override some UITableViewDataSource methods as needed. The DiffableDataSource.TableViewAdapterAdapter instance needs to be held on (retained) for as long as the UITableView‘s lifecycle.

self.dataSource = DiffableDataSource.TableViewAdapter<Person>(
    tableView: self.tableView,
    dataStack: CoreStoreDefaults.dataStack,
    cellProvider: { (tableView, indexPath, person) in
        let cell = tableView.dequeueReusableCell(withIdentifier: "PersonCell") as! PersonCell
        cell.setPerson(person)
        return cell
    }
)

The dataSource can then apply changes from a ListPublisher as shown:

listPublisher.addObserver(self) { [weak self] (listPublisher) in
   self?.dataSource?.apply(
       listPublisher.snapshot,
       animatingDifferences: true
   )
}

DiffableDataSource.TableViewAdapter fully handles the reload animations.

See also

CoreStore’s DiffableDataSource implementation is based on https://github.com/ra1028/DiffableDataSources

Public

  • Initializes the DiffableDataSource.TableViewAdapter. This instance needs to be held on (retained) for as long as the UITableView‘s lifecycle.

    self.dataSource = DiffableDataSource.TableViewAdapter<Person>(
        tableView: self.tableView,
        dataStack: CoreStoreDefaults.dataStack,
        cellProvider: { (tableView, indexPath, person) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "PersonCell") as! PersonCell
            cell.setPerson(person)
            return cell
        }
    )
    

    Declaration

    Swift

    public init(
        tableView: UITableView,
        dataStack: DataStack,
        cellProvider: @escaping (UITableView, IndexPath, O) -> UITableViewCell?
    )

    Parameters

    tableView

    the UITableView to set the dataSource of. This instance is not retained by the DiffableDataSource.TableViewAdapter.

    dataStack

    the DataStack instance that the dataSource will fetch objects from

    cellProvider

    a closure that configures and returns the UITableViewCell for the object

  • The target UITableView

    Declaration

    Swift

    public var tableView: UITableView? { get }