Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import CoreData
- import UIKit
- protocol TableViewManagerDelegate: AnyObject {
- associatedtype Cell: UITableViewCell
- associatedtype Header: UITableViewHeaderFooterView
- associatedtype Footer: UITableViewHeaderFooterView
- associatedtype Object: NSManagedObject
- func configureCell(_ cell: Cell, for object: Object)
- func configureHeader(_ header: Header, for title: String?)
- func configureFooter(_ footer: Footer, for title: String?)
- }
- // MARK: -
- extension TableViewManagerDelegate {
- func configureHeader(
- _ header: UITableViewHeaderFooterView,
- for title: String?
- ) { }
- func configureFooter(
- _ footer: UITableViewHeaderFooterView,
- for title: String?
- ) { }
- }
- // MARK: -
- class TableViewManager<Delegate: TableViewManagerDelegate>:
- NSObject,
- UITableViewDataSource,
- UITableViewDelegate,
- NSFetchedResultsControllerDelegate
- {
- // MARK: - Types
- typealias Cell = Delegate.Cell
- typealias Header = Delegate.Header
- typealias Footer = Delegate.Footer
- typealias Object = Delegate.Object
- // MARK: - Variables
- weak var delegate: Delegate?
- private let tableView: UITableView
- private let frc: NSFetchedResultsController<Object>
- // MARK: - Lifecycle
- init(
- tableView: UITableView,
- frc: NSFetchedResultsController<Object>
- ) {
- self.tableView = tableView
- self.frc = frc
- super.init()
- }
- // MARK: - Public
- func object(at indexPath: IndexPath) -> Object {
- return frc.object(at: indexPath)
- }
- func title(at section: Int) -> String? {
- return frc.sections?[safe: section]?.name ?? nil
- }
- // MARK: - UITableViewDataSource
- func numberOfSections(
- in tableView: UITableView
- ) -> Int {
- return frc.sections?.count ?? 0
- }
- func tableView(
- _ tableView: UITableView,
- numberOfRowsInSection section: Int
- ) -> Int {
- return frc.sections?[section].numberOfObjects ?? 0
- }
- func tableView(
- _ tableView: UITableView,
- cellForRowAt indexPath: IndexPath
- ) -> UITableViewCell {
- guard let cell = tableView.dequeueReusableCell(
- withIdentifier: Cell.name,
- for: indexPath
- ) as? Cell else {
- fatalError("Failed to get \(Cell.name)")
- }
- delegate?.configureCell(cell, for: object(at: indexPath))
- return cell
- }
- // MARK: - UITableViewDelegate
- func tableView(
- _ tableView: UITableView,
- viewForHeaderInSection section: Int
- ) -> UIView? {
- guard let header = tableView.dequeueReusableHeaderFooterView(
- withIdentifier: Header.name
- ) as? Header else {
- fatalError("Failed to get \(Header.name)")
- }
- delegate?.configureHeader(header, for: title(at: section))
- return header
- }
- func tableView(
- _ tableView: UITableView,
- viewForFooterInSection section: Int
- ) -> UIView? {
- guard let footer = tableView.dequeueReusableHeaderFooterView(
- withIdentifier: Footer.name
- ) as? Footer else {
- fatalError("Failed to get \(Footer.name)")
- }
- delegate?.configureFooter(footer, for: title(at: section))
- return footer
- }
- }
- // MARK: -
- class TestViewController: UIViewController, TableViewManagerDelegate {
- func configureCell(
- _ cell: UITableViewCell,
- for object: NSManagedObject
- ) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement