1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // ActionSheetItemHandler.swift
- // Sheeeeeeeeet
- //
- // Created by Daniel Saidi on 2017-11-24.
- // Copyright © 2017 Daniel Saidi. All rights reserved.
- //
- import UIKit
- open class ActionSheetItemHandler: NSObject {
-
-
- // MARK: - Initialization
-
- init(actionSheet: ActionSheet, handles itemType: ItemType) {
- self.actionSheet = actionSheet
- self.itemType = itemType
- }
-
-
- // MARK: - Enum
-
- public enum ItemType {
- case items, buttons
- }
-
-
- // MARK: - Properties
-
- private weak var actionSheet: ActionSheet?
-
- private var itemType: ItemType
-
- private var items: [ActionSheetItem] {
- switch itemType {
- case .buttons: return actionSheet?.buttons ?? []
- case .items: return actionSheet?.items ?? []
- }
- }
- }
- // MARK: - UITableViewDataSource
- extension ActionSheetItemHandler: UITableViewDataSource {
-
- public func item(at indexPath: IndexPath) -> ActionSheetItem {
- return items[indexPath.row]
- }
-
- public func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return items.count
- }
-
- public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- return item(at: indexPath).cell(for: tableView)
- }
-
- public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return CGFloat(item(at: indexPath).appearance.height)
- }
- }
- // MARK: - UITableViewDelegate
- extension ActionSheetItemHandler: UITableViewDelegate {
-
- public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- guard items.count > indexPath.row else { return }
- let item = self.item(at: indexPath)
- let cell = tableView.cellForRow(at: indexPath)
- tableView.deselectRow(at: indexPath, animated: true)
- guard let sheet = actionSheet else { return }
- item.handleTap(in: sheet, cell: cell)
- sheet.itemTapAction(item)
- }
- }
|