Latest 0.2.0
Homepage https://github.com/bynelus/PluggableTableView
License MIT
Platforms ios 9.0
Authors

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

  • [x] Support view models for a table view.
  • [x] Automatic registration of cell types and header / footer views.
  • [x] Support auto sizing cells and non-auto sizing cells.
  • [x] 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, [email protected]

License

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

Latest podspec

{
    "name": "PluggableTableView",
    "version": "0.2.0",
    "summary": "PluggableTableView provides a TableView to work easily with ViewModels.",
    "description": "PluggableTableView provides an UITableView that does all the work for you. The only thing you need to do is provide a list of view models.nThe registration of the cells, headers and footers will be done automatically.",
    "homepage": "https://github.com/bynelus/PluggableTableView",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "NielsKoole": "[email protected]"
    },
    "source": {
        "git": "https://github.com/bynelus/PluggableTableView.git",
        "tag": "0.2.0"
    },
    "social_media_url": "https://twitter.com/nielskoole",
    "platforms": {
        "ios": "9.0"
    },
    "ios": {
        "source_files": "PluggableTableView/Classes/**/*"
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This