Instantly share code, notes, and snippets.
Created
April 27, 2019 02:07
-
Save jkereako/e769c68e3424d95f9d87daa379bc4631 to your computer and use it in GitHub Desktop.
TestApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MenuTableViewCellModel.swift | |
// TestApp | |
// | |
// Created by Jeff Kereakoglow on 2/26/18. | |
// Copyright © 2018 Alexis Digital. All rights reserved. | |
// | |
import Foundation | |
import UIKit.UIViewController | |
struct MenuTableViewCellModel { | |
let title: String | |
let viewController: () -> UIViewController | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MenuTableViewController.swift | |
// TestApp | |
// | |
// Created by Jeff Kereakoglow on 2/26/18. | |
// Copyright © 2018 Alexis Digital. All rights reserved. | |
// | |
import UIKit | |
final class MenuTableViewController: UITableViewController { | |
var viewModel: [MenuTableViewCellModel]? | |
private let cellReuseIdentifier = "TableViewCell" | |
init() { | |
super.init(style: .grouped) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
title = "Test App" | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) | |
} | |
} | |
// MARK: - UITableViewDataSource | |
extension MenuTableViewController { | |
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) | |
-> String? { | |
return "Screens" | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return viewModel == nil ? 0 : 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return viewModel?.count ?? 0 | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) | |
-> UITableViewCell { | |
let cell = tableView.dequeueReusableCell( | |
withIdentifier: cellReuseIdentifier, for: indexPath | |
) | |
cell.textLabel?.text = viewModel?[indexPath.row].title ?? "" | |
return cell | |
} | |
} | |
// MARK: - UITableViewDelegate | |
extension MenuTableViewController { | |
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> | |
CGFloat { | |
return 56 | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
// Invoke the handler for the selected cell | |
guard let viewController = viewModel?[indexPath.row].viewController else { return } | |
show(viewController(), sender: self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MenuTableViewModelBuilder.swift | |
// TestApp | |
// | |
// Created by Jeff Kereakoglow on 2/26/18. | |
// Copyright © 2018 Alexis Digital. All rights reserved. | |
// | |
@testable import Core | |
final class MenuTableViewModelBuilder { | |
var viewModel: [MenuTableViewCellModel] { | |
return [ | |
MenuTableViewCellModel(title: "Edit Message Screen") { | |
return EditMessageViewController() | |
}, | |
MenuTableViewCellModel(title: "Welcome Screen") { | |
return WelcomeViewController() | |
}, | |
MenuTableViewCellModel(title: "Log In Screen") { | |
return LogInViewController() | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment