Latest 1.0.2
Homepage https://github.com/andzuz/RxTableViewControllable
License MIT
Platforms ios 10.0
Dependencies RxDataSources
Frameworks UIKit
Authors

Version
License
Platform

Requirements

Installation

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

pod 'RxTableViewControllable'

Author

Andrzej Zuzak, [email protected]

Usage

Quick start

Firstly, we want to create our UITableViewCell subclass (this library allows only table view cells created from code instead of the NIBs):

class SampleCell: UITableViewCell {

    private lazy var nameLabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configureUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("This init shouldn't be used")
    }

    func bind(to viewModel: SampleCellViewModel) {
        nameLabel.text = viewModel.name
    }

    private func configureUI() {
        addSubview(nameLabel)

        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    }
}

Then we need to create items with the data to fill all the cells, in our case it will be fake ViewModel from MVVM:

struct SampleCellViewModel {
    let name: String
}

Finally, we create UIViewController that needs to display UITableView:

class ViewController: UIViewController {

    // MARK: - RxTableViewControllable properties

    var dataProducer: BehaviorRelay<[SampleCellViewModel]> = BehaviorRelay<[SampleCellViewModel]>(value: [])
    var disposeBag = DisposeBag()
    var tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        configureUI()
        configureRxTableView() // this needs to be called
        fillWithData()
    }

    private func fillWithData() {
        let sampleViewModel = SampleCellViewModel(name: "John")
        dataProducer.accept([sampleViewModel])
    }

    private func configureUI() {
        view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            tableView.widthAnchor.constraint(equalTo: view.widthAnchor),
            tableView.heightAnchor.constraint(equalTo: view.heightAnchor)
        ])
    }
}

// MARK: - rx table view

extension ViewController: RxTableViewControllable, UITableViewDelegate {

    typealias Item = SampleCellViewModel
    typealias Cell = SampleCell

    func configureCell(_ cell: SampleCell, withItem item: SampleCellViewModel) {
        cell.bind(to: item)
    }
}

This library uses RxSwift and RxCocoa, so to provide data we use BehaviorRelay. When creating our controller we need to provide DisposeBag, UITableView and BehaviorRelay as the data producer – RxCocoa needs to be included for that to work. Then using typealias we estabilish what cell and item’s types are going to be and we provide a function to configure the cell with an item. There are also callbacks for tapping the cell.

TODO: support for sections in UITableView

License

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

Latest podspec

{
    "name": "RxTableViewControllable",
    "version": "1.0.2",
    "summary": "Protocol to easily create view controllers with the table views",
    "description": "Protocol to easily create view controllers with the table views. It uses RxDataSources under the hood.",
    "homepage": "https://github.com/andzuz/RxTableViewControllable",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "andzuz": "[email protected]"
    },
    "source": {
        "git": "https://github.com/andzuz/RxTableViewControllable.git",
        "tag": "1.0.2"
    },
    "platforms": {
        "ios": "10.0"
    },
    "source_files": "RxTableViewControllable/Classes/**/*",
    "dependencies": {
        "RxDataSources": [
            "~> 3.0"
        ]
    },
    "frameworks": "UIKit",
    "swift_version": "4.1"
}

Pin It on Pinterest

Share This