diff --git a/Sources/TableRowAction.swift b/Sources/TableRowAction.swift index c4bafdb..dac4b49 100644 --- a/Sources/TableRowAction.swift +++ b/Sources/TableRowAction.swift @@ -61,25 +61,40 @@ open class TableRowActionData where CellTy } } +private enum TableRowActionHandler where CellType.T == ItemType, CellType: UITableViewCell { + + case voidAction((TableRowActionData) -> Void) + case action((TableRowActionData) -> Any?) + + func invoke(item: ItemType, cell: UITableViewCell?, path: IndexPath) -> Any? { + + switch self { + case .voidAction(let handler): + return handler(TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil)) + case .action(let handler): + return handler(TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil)) + } + } +} + open class TableRowAction where CellType.T == ItemType, CellType: UITableViewCell { open let type: TableRowActionType - private let handler: ((_ data: TableRowActionData) -> Any?)? - + private let handler: TableRowActionHandler + public init(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData) -> Void) { self.type = type - self.handler = nil - //self.handler = handler + self.handler = .voidAction(handler) } public init(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData) -> T) { self.type = type - self.handler = handler + self.handler = .action(handler) } func invoke(item: ItemType, cell: UITableViewCell?, path: IndexPath) -> Any? { - return handler?(TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil)) + return handler.invoke(item: item, cell: cell, path: path) } }