Skip to content

Instantly share code, notes, and snippets.

@ivangodfather
Last active November 7, 2015 07:45
Show Gist options
  • Save ivangodfather/b3411f9af0ab78b38dd6 to your computer and use it in GitHub Desktop.
Save ivangodfather/b3411f9af0ab78b38dd6 to your computer and use it in GitHub Desktop.
class CarListViewController: UIViewController {
var presenter: CarListPresenter!
@IBOutlet weak var tableView: UITableView!
private var dataSource: CarListDataSource!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = CarListDataSource()
tableView.dataSource = dataSource
tableView.registerNib(UINib(nibName: CarListTableViewCell.reuseIdentifier, bundle: nil), forCellReuseIdentifier: CarListTableViewCell.reuseIdentifier)
presenter.viewDidLoad()
}
}
extension CarListViewController: CarlistUI {
func showCars(cars: [CarViewModel]) {
dataSource.cars = cars
tableView.reloadData()
}
}
----
class CarListPresenter {
private let ui : CarlistUI
private let useCase: GetCarList
init(ui: CarlistUI, useCase: GetCarList) {
self.ui = ui
self.useCase = useCase
}
func viewDidLoad() {
useCase.execute { result in
self.ui.showCars(result.value!)
}
}
}
---
import UIKit
class CarListDataSource: NSObject, UITableViewDataSource {
var cars: [CarViewModel]?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
guard let cars = cars else {
fatalError()
}
let cell = tableView.dequeueReusableCellWithIdentifier(CarListTableViewCell.reuseIdentifier, forIndexPath: indexPath) as! CarListTableViewCell
cell.configureWithCarViewModel(cars[indexPath.row])
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cars?.count ?? 0
}
}
-----
class CarListTableViewCell: UITableViewCell {
static let reuseIdentifier = "CarListTableViewCell"
@IBOutlet weak var brandLabel: UILabel!
func configureWithCarViewModel(carViewModel: CarViewModel) {
self.brandLabel.text = carViewModel.brand
}
}
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment