TableViewDataSource

public class TableViewDataSource<Delegate: TableViewDataSourceDelegate>: NSObject,
  UITableViewDataSource,
  NSFetchedResultsControllerDelegate

A data source for a UITableView that relies on a NSFetchedResultsController for model values. This design was heavily based on code from obj.io Core Data book.

  • Undocumented

    Declaration

    Swift

    public typealias Entity = Delegate.Entity
  • Undocumented

    Declaration

    Swift

    public typealias Cell = Delegate.Cell
  • Obtain the managed object for the currently selected row

    Declaration

    Swift

    public var selectedObject: Entity? { get }
  • Construct a new instance.

    Declaration

    Swift

    public required init(
      tableView: UITableView, cellIdentifier: String,
      fetchedResultsController: NSFetchedResultsController<Entity>, delegate: Delegate
    )

    Parameters

    tableView

    the UITableView that will show the rendered model instances

    cellIdentifier

    the identifier of the UITableViewCell to use for rendering

    fetchedResultsController

    the source of model instances from Core Data

    delegate

    the delegate for rendering and deletion handling

  • Obtain the number of model instances, or the number of rows in the UITableView.

    Declaration

    Swift

    public var count: Int { get }
  • Obtain the model instance for a given UITableView row.

    Declaration

    Swift

    public func object(at indexPath: IndexPath) -> Entity

    Parameters

    indexPath

    the row to fetch

    Return Value

    the found model instance

  • Change an existing Core Data fetch request and execute it.

    Declaration

    Swift

    public func reconfigureFetchRequest(_ configure: (NSFetchRequest<Entity>) -> Void)

    Parameters

    configure

    block to run to edit the request

UITableViewDataSource

  • Query for the number of rows in a table view section.

    Declaration

    Swift

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    Parameters

    tableView

    the UITableView being asked about

    section

    the section being asked about

    Return Value

    row count in the section

  • Obtain a formatted UITableViewCell for a specific row in a table view. The delegate’s configure method performs the necessary configuration on the cell before it is used.

    Declaration

    Swift

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
      -> UITableViewCell

    Parameters

    tableView

    the UITableView being worked on

    indexPath

    the index of the row being displayed

    Return Value

    the UITableViewCell to use to display the row

  • Query to find out if a row in a table view can be edited.

    Declaration

    Swift

    public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool

    Parameters

    tableView

    the UITableView being worked on

    indexPath

    the index of the row being asked about

    Return Value

    true if so

  • Perform an edit action on a specific row

    Declaration

    Swift

    public func tableView(
      _ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle,
      forRowAt indexPath: IndexPath
    )

    Parameters

    tableView

    the UITableView being worked on

    editingStyle

    the operation being performed. The only one supported is .delete

    indexPath

    the index of the row being edited

NSFetchedResultsControllerDelegate

  • Notification from NSFetchedResultsController that it is is going to make changes that affect the view

    Declaration

    Swift

    public func controllerWillChangeContent(
      _ controller: NSFetchedResultsController<NSFetchRequestResult>
    )

    Parameters

    controller

    the controller performing the work

  • Notification from NSFetchedResultsController about a change at a given index.

    Declaration

    Swift

    public func controller(
      _ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any,
      at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?
    )

    Parameters

    controller

    the controller performing the work

    anObject

    the object being affected

    indexPath

    the index of the object being affected

    type

    the type of change being performed

    newIndexPath

    the new index of the object after the operation (optional)

  • Notification from NSFetchedResultsController that all changes are done. Notify the delegate that the view was changed.

    Declaration

    Swift

    public func controllerDidChangeContent(
      _ controller: NSFetchedResultsController<NSFetchRequestResult>
    )

    Parameters

    controller

    the controller that performed the work