From 699d2aa00d0b89cc16dfc860a5a5a6f2ee04d651 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Sun, 12 Jun 2016 14:58:38 +0300 Subject: [PATCH] fix row actions --- TableKit/TableDirector.swift | 2 +- TableKit/TableRow.swift | 6 ++-- TableKit/TableRowAction.swift | 32 ++++++++++++++----- TableKit/TableSection.swift | 3 +- TableKit/Tablet.swift | 18 ----------- .../Controllers/MainController.swift | 8 +++-- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/TableKit/TableDirector.swift b/TableKit/TableDirector.swift index e77a671..b4f3ccb 100644 --- a/TableKit/TableDirector.swift +++ b/TableKit/TableDirector.swift @@ -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() diff --git a/TableKit/TableRow.swift b/TableKit/TableRow.swift index 620342a..770c686 100644 --- a/TableKit/TableRow.swift +++ b/TableKit/TableRow.swift @@ -41,7 +41,7 @@ public protocol Row: RowConfigurable, RowActionable { public class TableRow: Row { public let item: ItemType - private var actions = [String: RowAction]() + private lazy var actions = [String: TableRowAction]() public var reusableIdentifier: String { return CellType.reusableIdentifier() @@ -56,7 +56,9 @@ public class TableRow]? = nil) { + self.item = item + actions?.forEach { self.actions[$0.type.key] = $0 } } // MARK: - RowConfigurable - @@ -68,7 +70,7 @@ public class TableRow Any? { - return actions[action.key]?.invoke() + return actions[action.key]?.invoke(item: item, cell: cell, path: path) } public func hasAction(action: TableRowActionType) -> Bool { diff --git a/TableKit/TableRowAction.swift b/TableKit/TableRowAction.swift index 9c73c79..e55b881 100644 --- a/TableKit/TableRowAction.swift +++ b/TableKit/TableRowAction.swift @@ -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 { - 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: RowAction { +public class TableRowAction { public let type: TableRowActionType + private let handler: ((data: TableRowActionData) -> Any?) + + public init(_ type: TableRowActionType, handler: (data: TableRowActionData) -> Void) { - public init(_ type: TableRowActionType, handler: (row: TableRow) -> Void) { self.type = type + self.handler = handler } - public init(_ type: TableRowActionType, handler: (row: TableRow) -> T) { + public init(_ type: TableRowActionType, handler: (data: TableRowActionData) -> 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)) } } \ No newline at end of file diff --git a/TableKit/TableSection.swift b/TableKit/TableSection.swift index 9046fbc..9ec231f 100644 --- a/TableKit/TableSection.swift +++ b/TableKit/TableSection.swift @@ -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) + } } \ No newline at end of file diff --git a/TableKit/Tablet.swift b/TableKit/Tablet.swift index fb22bde..6e96c77 100644 --- a/TableKit/Tablet.swift +++ b/TableKit/Tablet.swift @@ -25,24 +25,6 @@ struct TabletNotifications { } -public class ActionData { - - 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. diff --git a/TableKitDemo/Classes/Presentation/Controllers/MainController.swift b/TableKitDemo/Classes/Presentation/Controllers/MainController.swift index 9a2f174..b6bad3e 100644 --- a/TableKitDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TableKitDemo/Classes/Presentation/Controllers/MainController.swift @@ -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 "" + })