Skip to content

bynelus/PluggableTableView

Repository files navigation

PluggableTableView

CI Status Version License Platform Twitter

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Swift 4.0
  • iOS 9.0

Installation

PluggableTableView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'PluggableTableView'

Summary

Showing a list is something you can see in almost every app. PluggableTableView becomes handy if you want to show a simple list that should be extendable and clean. You can add all kinds of cells, headers and footers by just providing a specific view model. View Models seperates business logic from the controller and can easily be tested. This configuration is open for extendability and close for modification.

Features

  • Support view models for a table view.
  • Automatic registration of cell types and header / footer views.
  • Support auto sizing cells and non-auto sizing cells.
  • Support xib cells.
  • Support non-xib cells.

Usage

Setup the view

import PluggableTableView

class ViewController: UIViewController
{
    @IBOutlet weak var tableView: PluggableTableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.autoSizingEnabled = true
        tableView.pluggableDataSource = self
    }
}

extension ViewController: PluggableTableViewDataSource
{
    func pluggableSections() -> [PluggableTableSection] {
        //TODO: Needs to be implemented
    }
}

Create view models

Create a section view model that specifies which header / footer and cells to show.

class DefaultSection: PluggableTableSection
{
    let header: PluggableTableHeaderFooter?
    let viewModels: [PluggableTableViewModel]
    let footer: PluggableTableHeaderFooter?
    
    init(viewModels: [PluggableTableViewModel], header: PluggableTableHeaderFooter? = nil, footer: PluggableTableHeaderFooter? = nil) {
        self.viewModels = viewModels
        self.header = header
        self.footer = footer
    }
}

Then you only need to specify your cell view model, which can be literally anything. Below just an example.

class CellTitleViewModel: PluggableTableViewModel
{
    typealias C = YourTitleCell

    let model: Model // The model you want to map to this view model.
    var cellType: UITableViewCell.Type = C.self
    
    init(model: Model) {
        self.model = model
    }
    
    func cell(from tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellType.identifier, for: indexPath) as! C
        cell.title = "\(model.title) \(model.something)"
        return cell
    }
    
    func height(for width: CGFloat) -> CGFloat? { 
    	// We return `nil` here because the cell has been setup for auto-sizing.
        return nil
    }
}

Provide data to table view

import PluggableTableView

class ViewController: UIViewController
{
    // Configure the view..
}

extension ViewController: PluggableTableViewDataSource
{
    func pluggableSections() -> [PluggableTableSection] {
        let viewModels = models.map({ CellTitleViewModel(model: $0) })
        let singleSection = DefaultSection(viewModels: viewModels) 
        return [singleSection]
    }
}

Author

NielsKoole, me@nielskoole.nl

License

PluggableTableView is available under the MIT license. See the LICENSE file for more info.

About

PluggableTableView provides a TableView to work easily with View Models.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages