Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MasterMenuController.swift
- // HeroTechUIDemo
- //
- // Created by Brian Drelling on 6/19/18.
- // Copyright © 2018 SpotHero. All rights reserved.
- //
- import Foundation
- import UIKit
- class MasterMenuController: UITableViewController {
- var sections = MenuSection.allCases
- // MARK: - Methods
- override func numberOfSections(in tableView: UITableView) -> Int {
- return sections.count
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: true)
- let item = sections[indexPath.section].rows[indexPath.row]
- switch item {
- case .alerts(.first):
- return
- case .alerts(.second):
- return
- case .modals(.first):
- return
- }
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return sections[section].rows.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath)
- cell.textLabel?.text = sections[indexPath.section].rows[indexPath.row].title
- return cell
- }
- override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- return sections[section].title
- }
- }
- // MARK: - Menu Configuration
- extension MasterMenuController {
- // Basic Menu Structure
- enum MenuSection: String, MenuReadable {
- case alerts = "Alerts"
- case modals = "Modals"
- static let allCases: [MenuSection] = [.alerts, .modals]
- var rows: [MenuItem] {
- return MenuItem.getItems(forSection: self)
- }
- }
- enum AlertsMenuRow: String, MenuReadable {
- case first = "First"
- case second = "Second"
- static let allCases: [AlertsMenuRow] = [.first]
- }
- enum ModalsMenuRow: String, MenuReadable {
- case first = "First"
- static let allCases: [ModalsMenuRow] = [.first]
- }
- // Allows friendly switch within didSelectRow function above
- // Switch in getItems will break if new items are added to MenuSection, alerting developer to update the MenuItem enum
- enum MenuItem {
- case alerts(AlertsMenuRow)
- case modals(ModalsMenuRow)
- var title: String {
- switch self {
- case .alerts(let row):
- return row.rawValue
- case .modals(let row):
- return row.rawValue
- }
- }
- static func getItems(forSection section: MenuSection) -> [MenuItem] {
- switch section {
- case .alerts:
- return AlertsMenuRow.allCases.map({ .alerts($0) })
- case .modals:
- return ModalsMenuRow.allCases.map({ .modals($0) })
- }
- }
- }
- }
- // MARK: - Convenience Protocols
- protocol MenuReadable {
- var rawValue: String { get }
- }
- extension MenuReadable {
- var title: String {
- return self.rawValue
- }
- }
Add Comment
Please, Sign In to add comment