diff --git a/README.md b/README.md index 72ea91a..23d35e5 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ ![Tablet](https://raw.githubusercontent.com/maxsokolov/tablet/assets/tablet.png)

+Swift 2 compatible Platform iOS -Swift 2.1 compatible +CocoaPods compatible License: MIT

@@ -70,17 +71,17 @@ import Tablet class MyTableViewCell : UITableViewCell, ConfigurableCell { - typealias Item = User - - static func reusableIdentifier() -> String { - return "reusable_id" - } + typealias Item = User - func configureWithItem(item: Item) { // item is user here + static func reusableIdentifier() -> String { + return "reusable_id" + } - textLabel?.text = item.username + func configureWithItem(item: Item) { // item is user here + + textLabel?.text = item.username detailTextLabel?.text = item.isActive ? "Active" : "Inactive" - } + } } ``` Once you've implemented the protocol, simply use the `TableConfigurableRowBuilder` to build cells: @@ -107,10 +108,11 @@ let rowBuilder = TableRowBuilder(items: [user1, user2, us } .action(.click) { data in - + } - .action(.willDisplay) { data in - + .action(.shouldHighlight) { data in + + return false } ``` @@ -124,7 +126,7 @@ class MyTableViewCell : UITableViewCell { @IBAction func buttonClicked(sender: UIButton) { - Action(key: kMyAction, sender: self, userInfo: nil).trigger() + Action(key: kMyAction, sender: self, userInfo: nil).perform() } } ``` @@ -144,6 +146,31 @@ let rowBuilder = TableConfigurableRowBuilder(items: users } ``` +## Extensibility + +If you find that Tablet is not provide an action you need, for example you need UITableViewDelegate's `didEndDisplayingCell` method and it's not out of the box, +simply provide an extension for `TableDirector` as follow: +```swift +import Tablet + +let kTableDirectorDidEndDisplayingCell = "enddisplaycell" + +extension TableDirector { + + public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { + + performAction(.custom(kTableDirectordidEndDisplayingCell), cell: cell, indexPath: indexPath) + } +} +``` +Catch your action with row builder: +```swift +let rowBuilder = TableConfigurableRowBuilder(items: users) + .action(kTableDirectordidEndDisplayingCell) { data in + + } +``` + ## License Tablet is available under the MIT license. See LICENSE for details. \ No newline at end of file diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index b8bb781..30c6daf 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -69,10 +69,10 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return sections[indexPath.section].builderAtIndex(indexPath.row)! } - public func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? { + public func performAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? { let builder = builderAtIndexPath(indexPath) - return builder.0.triggerAction(action, cell: cell, indexPath: indexPath, itemIndex: builder.1) + return builder.0.performAction(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(.custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1) + builder.0.performAction(.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(.configure, cell: cell, indexPath: indexPath, itemIndex: builder.1) + builder.0.performAction(.configure, cell: cell, indexPath: indexPath, itemIndex: builder.1) return cell } @@ -155,34 +155,33 @@ extension TableDirector { } public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - - - return triggerAction(.height, cell: nil, indexPath: indexPath) as? CGFloat ?? UITableViewAutomaticDimension + + return performAction(.height, cell: nil, indexPath: indexPath) as? CGFloat ?? UITableViewAutomaticDimension } public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) - if triggerAction(.click, cell: cell, indexPath: indexPath) != nil { + if performAction(.click, cell: cell, indexPath: indexPath) != nil { tableView.deselectRowAtIndexPath(indexPath, animated: true) } else { - triggerAction(.select, cell: cell, indexPath: indexPath) + performAction(.select, cell: cell, indexPath: indexPath) } } public func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { - triggerAction(.deselect, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) + performAction(.deselect, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) } public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { - - triggerAction(.willDisplay, cell: cell, indexPath: indexPath) + + performAction(.willDisplay, cell: cell, indexPath: indexPath) } public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool { - return triggerAction(.shouldHighlight, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? Bool ?? true + return performAction(.shouldHighlight, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? Bool ?? true } } \ No newline at end of file diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index a09de24..edd2854 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -107,7 +107,7 @@ public class TableRowBuilder : RowBuilder { // MARK: Triggers - public func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { + public func performAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { if let action = actions[actionType.key] { return action.call(ActionData(cell: cell as? C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex)) @@ -129,13 +129,13 @@ public class TableConfigurableRowBuilder AnyObject? { + public override func performAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { switch actionType { case .configure: (cell as? C)?.configureWithItem(items[itemIndex]) default: break } - return super.triggerAction(actionType, cell: cell, indexPath: indexPath, itemIndex: itemIndex) + return super.performAction(actionType, cell: cell, indexPath: indexPath, itemIndex: itemIndex) } } diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index 6ceeeaf..3331c53 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -86,7 +86,7 @@ public class Action { self.userInfo = userInfo } - public func trigger() { + public func perform() { NSNotificationCenter.defaultCenter().postNotificationName(kActionPerformedNotificationKey, object: self) } @@ -114,5 +114,5 @@ public protocol RowBuilder { var reusableIdentifier: String { get } var estimatedRowHeight: CGFloat { get } - func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? + func performAction(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 066f0b0..fe7b4a0 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/ConfigurableTableViewCell.swift b/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift index a83405f..07be0a2 100644 --- a/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift +++ b/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift @@ -29,6 +29,6 @@ class ConfigurableTableViewCell: UITableViewCell, ConfigurableCell { @IBAction func buttonClicked(sender: UIButton) { - Action(key: kConfigurableTableViewCellButtonClickedAction, sender: self).trigger() + Action(key: kConfigurableTableViewCellButtonClickedAction, sender: self).perform() } } \ No newline at end of file diff --git a/TabletDemo/TabletDemo/CustomTableActions.swift b/TabletDemo/TabletDemo/CustomTableActions.swift index 3c55cd9..c586a75 100644 --- a/TabletDemo/TabletDemo/CustomTableActions.swift +++ b/TabletDemo/TabletDemo/CustomTableActions.swift @@ -8,3 +8,13 @@ import UIKit import Foundation + +let kTableDirectorDidEndDisplayingCell = "enddisplaycell" + +extension TableDirector { + + public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { + + performAction(.custom(kTableDirectorDidEndDisplayingCell), cell: cell, indexPath: indexPath) + } +} \ No newline at end of file diff --git a/TabletDemo/TabletDemo/ViewController.swift b/TabletDemo/TabletDemo/ViewController.swift index 3638950..240941c 100644 --- a/TabletDemo/TabletDemo/ViewController.swift +++ b/TabletDemo/TabletDemo/ViewController.swift @@ -27,6 +27,10 @@ class ViewController: UIViewController { return false } + .action(kTableDirectorDidEndDisplayingCell) { data -> Void in + + print("end display: \(data.indexPath)") + } let configurableRowBuilder = TableConfigurableRowBuilder(items: ["5", "6", "7", "8"], estimatedRowHeight: 300) .action(.click) { data -> Void in