diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index f6cd97f..8209a5b 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -34,8 +34,6 @@ public enum ActionType : String { case configure = "_configure" case willDisplay = "_willDisplay" case shouldHighlight = "_shouldHighlight" - - } public struct ActionData { @@ -54,13 +52,39 @@ public struct ActionData { } } +public enum ActionResult { + + case Success(returnValue: AnyObject) + case Failure + + /// Returns true if the result is a success, false otherwise. + public var isSuccess: Bool { + switch self { + case .Success: + return true + case .Failure: + return false + } + } +} + +/** + A custom action that you can trigger from your cell. + You can eacily catch actions using a chaining manner with your row builder. +*/ public class Action { + /// The cell that triggers an action. public let cell: UITableViewCell + + /// The action unique key. public let key: String + + /// The custom user info. public let userInfo: [NSObject: AnyObject]? public init(key: String, sender: UITableViewCell, userInfo: [NSObject: AnyObject]? = nil) { + self.key = key self.cell = sender self.userInfo = userInfo @@ -72,6 +96,11 @@ public class Action { } } +public class ActionObject { + + +} + /** 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. @@ -93,7 +122,7 @@ public protocol ReusableRowBuilder { var numberOfRows: Int { get } var reusableIdentifier: String { get } - func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> Bool + func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult } /** @@ -167,15 +196,15 @@ public class TableRowBuilder : ReusableRowBuilder // MARK: Triggers - public func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> Bool { + public func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult { let actionData = ActionData(cell: cell as! C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex) if let block = actions[key] { block(data: actionData) - return true + return .Failure } - return false + return .Failure } } @@ -192,7 +221,7 @@ public class TableConfigurableRowBuilder Bool { + public override func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult { (cell as! C).configureWithItem(items[itemIndex]) @@ -259,7 +288,7 @@ public class TableSectionBuilder { } /** - A class that actualy handles table view's datasource and delegate. + Responsible for table view's datasource and delegate. */ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { @@ -306,12 +335,17 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return sections[indexPath.section].builderAtIndex(indexPath.row)! } - private func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> Bool { + private func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> ActionResult { let builder = builderAtIndexPath(indexPath) return builder.0.triggerAction(action.rawValue, cell: cell!, indexPath: indexPath, itemIndex: builder.1) } + private func trigger() -> AnyObject { + + return 100 + } + internal func didReceiveAction(notification: NSNotification) { if let action = notification.object as? Action, @@ -383,11 +417,21 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) - if triggerAction(.click, cell: cell, indexPath: indexPath) { + + let a = triggerAction(.click, cell: cell, indexPath: indexPath) + switch a { + case .Success(let returnValue): + print(returnValue) + case .Failure: + print("") + } + + + /*if triggerAction(.click, cell: cell, indexPath: indexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) } else { triggerAction(.select, cell: cell, indexPath: indexPath) - } + }*/ } public func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { @@ -402,6 +446,11 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool { - return false + return true + } + + public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + + return trigger() as! CGFloat } } \ 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 daf4a1a..14c3ca3 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.xcodeproj/xcuserdata/maxsokolov.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/TabletDemo/TabletDemo.xcodeproj/xcuserdata/maxsokolov.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/TabletDemo/TabletDemo.xcodeproj/xcuserdata/maxsokolov.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/TabletDemo/TabletDemo/ViewController.swift b/TabletDemo/TabletDemo/ViewController.swift index ca9f5e8..73f99c6 100644 --- a/TabletDemo/TabletDemo/ViewController.swift +++ b/TabletDemo/TabletDemo/ViewController.swift @@ -17,10 +17,10 @@ class ViewController: UIViewController { super.viewDidLoad() tableDirector = TableDirector(tableView: tableView) - + let rowBuilder = TableRowBuilder(items: [1, 2, 3, 4], id: "cell") .action(.configure) { data in - + data.cell.textLabel?.text = "\(data.item)" } @@ -35,7 +35,7 @@ class ViewController: UIViewController { } .action(.shouldHighlight) { _ in - return false + return 90 } let sectionBuilder = TableSectionBuilder(headerTitle: "Tablet", footerTitle: "Deal with table view like a boss.", rowBuilders: [rowBuilder, configurableRowBuilder])