diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 5135ea9..bcb0df1 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -23,7 +23,7 @@ import UIKit open class TableRow: Row where CellType: UITableViewCell { open let item: CellType.T - private lazy var actions = [String: RowAction]() + private lazy var actions = [String: [RowAction]]() private(set) open var editingActions: [UITableViewRowAction]? open var hashValue: Int { @@ -46,11 +46,11 @@ open class TableRow: Row where CellType: UITableView return CellType.self } - public init(item: CellType.T, actions: [RowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { + public init(item: CellType.T, actions: [TableRowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { self.item = item self.editingActions = editingActions - actions?.forEach { self.actions[$0.type.key] = $0 } + actions?.forEach { on($0) } } // MARK: - RowConfigurable - @@ -62,7 +62,8 @@ open class TableRow: Row where CellType: UITableView // MARK: - RowActionable - open func invoke(_ action: TableRowActionType, cell: UITableViewCell?, path: IndexPath) -> Any? { - return actions[action.key]?.invokeActionOn(cell: cell, item: item, path: path) + + return actions[action.key]?.flatMap({ $0.invokeActionOn(cell: cell, item: item, path: path) }).last } open func hasAction(_ action: TableRowActionType) -> Bool { @@ -80,17 +81,20 @@ open class TableRow: Row where CellType: UITableView // MARK: - actions - @discardableResult - open func on(_ action: RowAction) -> Self { + open func on(_ action: TableRowAction) -> Self { - actions[action.type.key] = action + if actions[action.type.key] == nil { + actions[action.type.key] = [RowAction]() + } + actions[action.type.key]?.append(action) + return self } - + @discardableResult open func on(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData) -> T) -> Self { - actions[type.key] = TableRowAction(type, handler: handler) - return self + return on(TableRowAction(type, handler: handler)) } // MARK: - deprecated actions - @@ -98,16 +102,14 @@ open class TableRow: Row where CellType: UITableView @available(*, deprecated, message: "Use 'on' method instead") @discardableResult open func action(_ action: TableRowAction) -> Self { - - actions[action.type.key] = action - return self + + return on(action) } @available(*, deprecated, message: "Use 'on' method instead") @discardableResult open func action(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData) -> T) -> Self { - actions[type.key] = TableRowAction(type, handler: handler) - return self + return on(TableRowAction(type, handler: handler)) } } diff --git a/Sources/TableRowAction.swift b/Sources/TableRowAction.swift index 1698226..38607c1 100644 --- a/Sources/TableRowAction.swift +++ b/Sources/TableRowAction.swift @@ -45,7 +45,8 @@ private enum TableRowActionHandler where CellType: U switch self { case .voidAction(let handler): - return handler(actionData) + handler(actionData) + return nil case .action(let handler): return handler(actionData) }