Latest 0.1.3
Homepage https://github.com/OrenRosen/AutoCellFactory
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

AutoCellFactory is based on Model-View-ViewModel (MVVM) architacture, which basically means that the cell owns a ViewModel (called a presenter), which holds the model, and when the AutoCellFactory sets the model, the presenter automatically calls reloadView and triggers the configureView at the Cell. A flappy diagram will look like this:

In the configureView, the cell configures itself by asking the presenter the fields it needs.

Example

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

Requirements

  • swift 2.1
  • iOS 8.0

Installation

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

pod "AutoCellFactory"

How To Use

Example for presenting UITableViewCell with Model by a Presenter

You’ll need to add the following object:

  • Model – can be a struct or a class, of any type
  • Presenter – inherits from ACBasePresenter<[YOUR MODEL TYPE]>
  • Cell – inherits from ACBasicCell
  • xib – the nib of your custom class, set the name and the reuse identifier as your cell name

The Model

struct SomeModel { 
    var name: String = ""
}

The Presenter

Create a presenter for the cell, subclass of ACBasePresenter, with a generic type as your model. In this case – SomeModel.

The presenter is the only one that will know about the model, and will give the cell the configuration based on this model. Its superclass will take care for automatic reloading the view when the model is set

It must include required init(), so ACBasicCell, which owns the presenter, could automatically initial it, without involving the subclass

class SomeCellPresenter: ACBasePresenter<SomeModel> {
    required init() {}
}

The cell

Create a subclass of ACBasicCell, with a generic type as your presenter. In this case – SomeCellPresenter.

class SomeCell: ACBasicCell<SomeCellPresenter> {
    override func configureCell() {
        // Do some configuration for your cell
    }
}

The generic type will promise that the superclass will init the presenter with this type, and will trigger configureCell automatically when the model is set

The nib

Be sure you create a nib file for that cell, named exactly like the cell, and set the reuse identifier in the same name

Factory part

Now its only left to actually show the cell using the AutoCellFactory

Register

create an instance of AutoCellFactory:

lazy var acFactory: AutoCellFactory = AutoCellFactory(tableView: self.tableView, delegate: self.viewModel)

The delegate needs to conforms to AutoCellFactoryViewDelegate, and to implement the method:

func modelForIndexPath(indexPath: NSIndexPath) -> Any?

Call register method at your AutoCellFactory instance:

public func registerForNib(cellsAndModels: [AutoCellFactoryRegistrationType])

Pass the tableView, and an array of AutoCellFactoryRegistrationType

public typealias AutoCellFactoryRegistrationType = (cellType: ACBasicCellPresenterHolder.Type, modelType: Any.Type)

This way the factory knows which model belongs to each cell, for example:

acFactory.registerForNib([(SomeCell.self, SomeModel.self)])

And in cellForRow, just return the cell from the acFactory:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return acFactory.getCell(forIndexPath: indexPath)
}

That’s it 💃

Author

Oren Rosenblum, [email protected]

License

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

Latest podspec

{
    "name": "AutoCellFactory",
    "version": "0.1.3",
    "summary": "Auto Create Cells.",
    "description": "AutoCellFactory is simple to use, all it does it auto deques the cell and configure it, using the MVVM architacture.",
    "homepage": "https://github.com/OrenRosen/AutoCellFactory",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Oren Rosenblum": "[email protected]"
    },
    "source": {
        "git": "https://github.com/OrenRosen/AutoCellFactory.git",
        "tag": "0.1.3"
    },
    "documentation_url": "https://github.com/OrenRosen/AutoCellFactory/blob/master/README.md",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "AutoCellFactory/Classes/**/*"
}

Pin It on Pinterest

Share This