readme updated, some refactoring
This commit is contained in:
parent
d0773704c4
commit
e25b7d759e
53
README.md
53
README.md
|
|
@ -1,8 +1,9 @@
|
|||

|
||||
|
||||
<p align="left">
|
||||
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift2-compatible-4BC51D.svg?style=flat" alt="Swift 2 compatible" /></a>
|
||||
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" />
|
||||
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift2.1-compatible-4BC51D.svg?style=flat" alt="Swift 2.1 compatible" /></a>
|
||||
<a href="https://cocoapods.org/pods/tablet"><img src="https://img.shields.io/badge/pod-0.1.0-blue.svg" alt="CocoaPods compatible" /></a>
|
||||
<a href="https://raw.githubusercontent.com/maxsokolov/tablet/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
|
||||
</p>
|
||||
|
||||
|
|
@ -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<User, MyTableViewCell>(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<User, MyTableViewCell>(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<User, MyTableViewCell>(items: users)
|
||||
.action(kTableDirectordidEndDisplayingCell) { data in
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Tablet is available under the MIT license. See LICENSE for details.
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -107,7 +107,7 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : 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<I, C: ConfigurableCell where C.Item ==
|
|||
super.init(items: items, id: C.reusableIdentifier(), estimatedRowHeight: estimatedRowHeight)
|
||||
}
|
||||
|
||||
public override func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> 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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
}
|
||||
Binary file not shown.
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -27,6 +27,10 @@ class ViewController: UIViewController {
|
|||
|
||||
return false
|
||||
}
|
||||
.action(kTableDirectorDidEndDisplayingCell) { data -> Void in
|
||||
|
||||
print("end display: \(data.indexPath)")
|
||||
}
|
||||
|
||||
let configurableRowBuilder = TableConfigurableRowBuilder<String, ConfigurableTableViewCell>(items: ["5", "6", "7", "8"], estimatedRowHeight: 300)
|
||||
.action(.click) { data -> Void in
|
||||
|
|
|
|||
Loading…
Reference in New Issue