Skip to content

Latest commit

 

History

History
84 lines (53 loc) · 2.38 KB

README.md

File metadata and controls

84 lines (53 loc) · 2.38 KB

ListController

Platform iOS Language: Swift 5 CocoaPods compatible SwiftPM compatible License: MIT

Provides an abstraction layer to deal with listable data. It's a simpler and faster way to build table views on top of this than from scratch.

Usage

1. Setup models and reusable views

1.1 Model setup

Item for cells must adopt Hashable protocol.

1.2 Cells setup

Table view cell should conform to Configurable protocol in order to receive cell item for setup.

extension Cell: Configurable {

    public func setup(with item: User) {

        textLabel?.text = item.name
    }
}

Sometimes you need to set delegate for cell, header or footer. For that purpose table adapter has sender property, which will be passed to configurable view, that adopts SenderConfigurable protocol.

extension Cell: SenderConfigurable {

    func setup(with item: Item, sender: ViewController) {

        textLabel?.text = object.name
        delegate = sender
    }
}

2. Use TableController as parent class

You must provide tableView property.

class ExamplesViewController: TableController<Int, Example> {

    override var tableView: UITableView { examplesTableView }
    
    private let examplesTableView = UITableView(frame: .zero, style: .grouped)
    private let items: [Example] = [ ... ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Setup table
        view.addSubview(tableView)
        tableView.register(ExampleCell.self, forCellReuseIdentifier: defaultCellReuseIdentifier)

        apply(items: items)
    }
    
    func apply(items: [RowItem], animated: Bool = false) {
        var snapshot = snapshot

        snapshot.deleteAllItems()
        snapshot.appendSections([0])
        snapshot.appendItems(items, toSection: 0)

        apply(snapshot, animating: animated)
    }
}

Requirements

  • Swift 5.1+
  • iOS 13.0+