value actions

This commit is contained in:
Max Sokolov 2016-06-09 19:34:28 +03:00
parent bf01dc638a
commit 708f164c96
4 changed files with 38 additions and 15 deletions

View File

@ -20,13 +20,21 @@
import UIKit
public protocol Row {
public protocol RowConfigurable {
func configure(cell: UITableViewCell)
}
public protocol RowActionable {
func invoke(action: TableRowActionType, cell: UITableViewCell?, path: NSIndexPath) -> Any?
}
public protocol Row: RowConfigurable, RowActionable {
var reusableIdentifier: String { get }
var estimatedHeight: CGFloat { get }
var defaultHeight: CGFloat { get }
func configure(cell: UITableViewCell)
}
public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: Row {
@ -50,14 +58,23 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
self.item = item
}
// MARK: - RowConfigurable -
public func configure(cell: UITableViewCell) {
(cell as? CellType)?.configure(item)
}
// MARK: - RowActionable -
public func invoke(action: TableRowActionType, cell: UITableViewCell?, path: NSIndexPath) -> Any? {
return nil
}
// MARK: - actions -
public func addAction(action: TableRowAction<ItemType, CellType>) {
public func action(action: TableRowAction<ItemType, CellType>) -> Self {
return self
}
}

View File

@ -20,7 +20,7 @@
import UIKit
public enum TableActionType {
public enum TableRowActionType {
case Click
case Custom(String)
@ -32,10 +32,11 @@ protocol RowAction {
public class TableRowAction<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: RowAction {
public init(_ action: TableActionType, handler: (row: TableRow<ItemType, CellType>) -> Void) {
public init(_ action: ActionType, handler: (row: TableRow<ItemType, CellType>) -> Void) {
}
public init<T>(_ action: ActionType, handler: (row: TableRow<ItemType, CellType>) -> T) {
}
}

View File

@ -30,7 +30,7 @@ class MainController: UIViewController {
let a = TableRowAction<String, StoryboardImageTableViewCell>(.Click) {
let a = TableRowAction<String, StoryboardImageTableViewCell>(.click) {
(row) in
}
@ -41,10 +41,15 @@ class MainController: UIViewController {
let row2 = TableRow<String, StoryboardImageTableViewCell>(item: "2")
let row3 = TableRow<String, StoryboardImageTableViewCell>(item: "3", actions: [a])
row1
.action(TableRowAction(.click) { (row) in
.addAction(TableRowAction(.Click) { (row) in
})
.action(TableRowAction(.click) { (row) -> String in
return ""
})