diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 5e81f42..6c1eda9 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -25,7 +25,7 @@ import Foundation Responsible for table view's datasource and delegate. */ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { - + private weak var tableView: UITableView! private var sections = [TableSectionBuilder]() @@ -72,7 +72,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate private func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? { let builder = builderAtIndexPath(indexPath) - return builder.0.triggerAction(action.rawValue, cell: cell, indexPath: indexPath, itemIndex: builder.1) + return builder.0.triggerAction(action, cell: cell, indexPath: indexPath, itemIndex: builder.1) } internal func didReceiveAction(notification: NSNotification) { @@ -80,7 +80,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate if let action = notification.object as? Action, indexPath = tableView.indexPathForCell(action.cell) { let builder = builderAtIndexPath(indexPath) - builder.0.triggerAction(action.key, cell: action.cell, indexPath: indexPath, itemIndex: builder.1) + builder.0.triggerAction(.custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1) } } @@ -102,7 +102,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate let cell = tableView.dequeueReusableCellWithIdentifier(builder.0.reusableIdentifier, forIndexPath: indexPath) - builder.0.triggerAction(ActionType.configure.rawValue, cell: cell, indexPath: indexPath, itemIndex: builder.1) + builder.0.triggerAction(.configure, cell: cell, indexPath: indexPath, itemIndex: builder.1) return cell } @@ -144,7 +144,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate // MARK: UITableViewDelegate - actions public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - + let cell = tableView.cellForRowAtIndexPath(indexPath) if triggerAction(.click, cell: cell, indexPath: indexPath) != nil { diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index 6c3a27c..66a2924 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -21,12 +21,12 @@ import UIKit import Foundation -internal enum ActionHandler { +internal enum ActionHandler { - case actionBlock((data: ActionData) -> Void) - case actionReturnBlock((data: ActionData) -> AnyObject) + case actionBlock((data: ActionData) -> Void) + case actionReturnBlock((data: ActionData) -> AnyObject) - func call(data: ActionData) -> AnyObject { + func call(data: ActionData) -> AnyObject { switch (self) { case .actionBlock(let closure): @@ -67,6 +67,8 @@ public class TableRowBuilder : RowBuilder { self.items.appendContentsOf(items!) } } + + // MARK: Items manipulation public func appendItems(items: [I]) { @@ -86,23 +88,23 @@ public class TableRowBuilder : RowBuilder { return self } - public func action(key: ActionType, action: (data: ActionData) -> Void) -> Self { + public func action(actionType: ActionType, action: (data: ActionData) -> Void) -> Self { - actions[key.rawValue] = .actionBlock(action) + actions[actionType.key] = .actionBlock(action) return self } - public func action(key: ActionType, action: (data: ActionData) -> AnyObject) -> Self { + public func action(actionType: ActionType, action: (data: ActionData) -> AnyObject) -> Self { - actions[key.rawValue] = .actionReturnBlock(action) + actions[actionType.key] = .actionReturnBlock(action) return self } // MARK: Triggers - public func triggerAction(key: String, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { + public func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { - if let action = actions[key] { + if let action = actions[actionType.key] { return action.call(ActionData(cell: cell as? C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex)) } return nil @@ -122,10 +124,10 @@ public class TableConfigurableRowBuilder AnyObject? { - + public override func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { + (cell as? C)?.configureWithItem(items[itemIndex]) - return super.triggerAction(key, cell: cell, indexPath: indexPath, itemIndex: itemIndex) + return super.triggerAction(actionType, cell: cell, indexPath: indexPath, itemIndex: itemIndex) } } diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index d9fbf9e..5c6f0ee 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -24,20 +24,31 @@ import Foundation internal let kActionPerformedNotificationKey = "_action" /** - Built in actions that Tablet provides. + The actions that Tablet provides. */ -public enum ActionType : String { +public enum ActionType { - case click = "_click" - case select = "_select" - case deselect = "_deselect" - case configure = "_configure" - case willDisplay = "_willDisplay" - case shouldHighlight = "_shouldHighlight" - case height = "_height" + case click + case select + case deselect + case configure + case willDisplay + case shouldHighlight + case height + case custom(String) + + var key: String { + + switch (self) { + case .custom(let str): + return str + default: + return "_\(self)" + } + } } -public struct ActionData { +public class ActionData { public let cell: C? public let item: I @@ -83,7 +94,7 @@ public class Action { /** If you want to delegate your cell configuration logic to cell itself (with your view model or even model) than - just provide an implementation of this protocol for your cell. Enjoy strong typisation. + just provide an implementation of this protocol for your cell. Enjoy safe-typisation. */ public protocol ConfigurableCell { @@ -102,5 +113,5 @@ public protocol RowBuilder { var numberOfRows: Int { get } var reusableIdentifier: String { get } - func triggerAction(key: String, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? + func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? } \ No newline at end of file diff --git a/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate b/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate index 0a16516..701e1e2 100644 Binary files a/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate and b/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TabletDemo/TabletDemo/ViewController.swift b/TabletDemo/TabletDemo/ViewController.swift index 842f30e..fd74961 100644 --- a/TabletDemo/TabletDemo/ViewController.swift +++ b/TabletDemo/TabletDemo/ViewController.swift @@ -37,7 +37,7 @@ class ViewController: UIViewController { data.cell!.textLabel?.text = "" - print("custom action indexPath: \(data.indexPath), item: \(data.item)") + print("click action indexPath: \(data.indexPath), item: \(data.item)") } .action(kConfigurableTableViewCellButtonClickedAction) { data in