fix row actions
This commit is contained in:
parent
0463cfdf40
commit
699d2aa00d
|
|
@ -26,8 +26,8 @@ import UIKit
|
|||
public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
|
||||
|
||||
public private(set) weak var tableView: UITableView?
|
||||
private weak var scrollDelegate: UIScrollViewDelegate?
|
||||
public private(set) var sections = [TableSection]()
|
||||
private weak var scrollDelegate: UIScrollViewDelegate?
|
||||
|
||||
public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil) {
|
||||
super.init()
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ public protocol Row: RowConfigurable, RowActionable {
|
|||
public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: Row {
|
||||
|
||||
public let item: ItemType
|
||||
private var actions = [String: RowAction]()
|
||||
private lazy var actions = [String: TableRowAction<ItemType, CellType>]()
|
||||
|
||||
public var reusableIdentifier: String {
|
||||
return CellType.reusableIdentifier()
|
||||
|
|
@ -56,7 +56,9 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
|
|||
}
|
||||
|
||||
public init(item: ItemType, actions: [TableRowAction<ItemType, CellType>]? = nil) {
|
||||
|
||||
self.item = item
|
||||
actions?.forEach { self.actions[$0.type.key] = $0 }
|
||||
}
|
||||
|
||||
// MARK: - RowConfigurable -
|
||||
|
|
@ -68,7 +70,7 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
|
|||
// MARK: - RowActionable -
|
||||
|
||||
public func invoke(action: TableRowActionType, cell: UITableViewCell?, path: NSIndexPath) -> Any? {
|
||||
return actions[action.key]?.invoke()
|
||||
return actions[action.key]?.invoke(item: item, cell: cell, path: path)
|
||||
}
|
||||
|
||||
public func hasAction(action: TableRowActionType) -> Bool {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ public enum TableRowActionType {
|
|||
case shouldHighlight
|
||||
case height
|
||||
case custom(String)
|
||||
|
||||
|
||||
var key: String {
|
||||
|
||||
switch (self) {
|
||||
|
|
@ -43,26 +43,42 @@ public enum TableRowActionType {
|
|||
}
|
||||
}
|
||||
|
||||
protocol RowAction {
|
||||
public class TableRowActionData<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell> {
|
||||
|
||||
func invoke() -> Any?
|
||||
public let item: ItemType
|
||||
public let cell: CellType?
|
||||
public let path: NSIndexPath
|
||||
public let userInfo: [NSObject: AnyObject]?
|
||||
|
||||
init(item: ItemType, cell: CellType?, path: NSIndexPath, userInfo: [NSObject: AnyObject]?) {
|
||||
|
||||
self.item = item
|
||||
self.cell = cell
|
||||
self.path = path
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
}
|
||||
|
||||
public class TableRowAction<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: RowAction {
|
||||
public class TableRowAction<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell> {
|
||||
|
||||
public let type: TableRowActionType
|
||||
private let handler: ((data: TableRowActionData<ItemType, CellType>) -> Any?)
|
||||
|
||||
public init(_ type: TableRowActionType, handler: (data: TableRowActionData<ItemType, CellType>) -> Void) {
|
||||
|
||||
public init(_ type: TableRowActionType, handler: (row: TableRow<ItemType, CellType>) -> Void) {
|
||||
self.type = type
|
||||
self.handler = handler
|
||||
}
|
||||
|
||||
public init<T>(_ type: TableRowActionType, handler: (row: TableRow<ItemType, CellType>) -> T) {
|
||||
public init<T>(_ type: TableRowActionType, handler: (data: TableRowActionData<ItemType, CellType>) -> T) {
|
||||
|
||||
self.type = type
|
||||
self.handler = handler
|
||||
}
|
||||
|
||||
// MARK: - RowAction -
|
||||
|
||||
func invoke() -> Any? {
|
||||
return nil
|
||||
func invoke(item item: ItemType, cell: UITableViewCell?, path: NSIndexPath) -> Any? {
|
||||
return handler(data: TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil))
|
||||
}
|
||||
}
|
||||
|
|
@ -74,7 +74,6 @@ public class TableSection {
|
|||
|
||||
public func append(rows rows: [Row]) {
|
||||
|
||||
//if let director = tableDirector { rows.forEach { $0.willUpdateDirector(director) } }
|
||||
//builders.appendContentsOf(rows)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -25,24 +25,6 @@ struct TabletNotifications {
|
|||
}
|
||||
|
||||
|
||||
public class ActionData<DataType, CellType> {
|
||||
|
||||
public let cell: CellType?
|
||||
public let item: DataType
|
||||
public let itemIndex: Int
|
||||
public let indexPath: NSIndexPath
|
||||
public let userInfo: [NSObject: AnyObject]?
|
||||
|
||||
init(cell: CellType?, indexPath: NSIndexPath, item: DataType, itemIndex: Int, userInfo: [NSObject: AnyObject]?) {
|
||||
|
||||
self.cell = cell
|
||||
self.indexPath = indexPath
|
||||
self.item = item
|
||||
self.itemIndex = itemIndex
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
A custom action that you can trigger from your cell.
|
||||
|
|
|
|||
|
|
@ -45,15 +45,17 @@ class MainController: UIViewController {
|
|||
|
||||
|
||||
row1
|
||||
.action(TableRowAction(.click) { (row) in
|
||||
.action(TableRowAction(.shouldHighlight) { (data) -> Bool in
|
||||
|
||||
print("1")
|
||||
|
||||
return false
|
||||
})
|
||||
.action(TableRowAction(.click) { (row) -> String in
|
||||
.action(TableRowAction(.click) { (data) in
|
||||
|
||||
print("2")
|
||||
|
||||
return ""
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue