From bba29151ec1545f3d2a3050e2ba8daee6edcb460 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Tue, 24 May 2016 22:51:04 +0300 Subject: [PATCH] first try prototype cells --- Tablet/RowBuilder.swift | 10 +++-- Tablet/TableDirector.swift | 12 ++--- Tablet/TableRowBuilder.swift | 44 ++++++++++++++++--- Tablet/TableSectionBuilder.swift | 4 +- .../Controllers/MainController.swift | 2 +- 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/Tablet/RowBuilder.swift b/Tablet/RowBuilder.swift index 4a07be7..ced4fb6 100644 --- a/Tablet/RowBuilder.swift +++ b/Tablet/RowBuilder.swift @@ -22,13 +22,15 @@ import UIKit public protocol RowBuilder { - var reusableIdentifier: String { get } - var numberOfRows: Int { get } + var tableDirector: TableDirector? { get } + var reusableIdentifier: String { get } + + var numberOfRows: Int { get } var estimatedRowHeight: CGFloat { get } - func rowHeight(index: Int) -> CGFloat + func willUpdateDirector(director: TableDirector?) + func rowHeight(index: Int) -> CGFloat func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? - func registerCell(inTableView tableView: UITableView) } \ No newline at end of file diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 15109bf..0ce4c13 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -25,16 +25,16 @@ import UIKit */ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { - public unowned let tableView: UITableView + public private(set) weak var tableView: UITableView? public weak var scrollDelegate: UIScrollViewDelegate? private var sections = [TableSectionBuilder]() public init(tableView: UITableView) { + super.init() self.tableView = tableView - super.init() - self.tableView.delegate = self - self.tableView.dataSource = self + self.tableView?.delegate = self + self.tableView?.dataSource = self NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didReceiveAction), name: TabletNotifications.CellAction, object: nil) } @@ -77,7 +77,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate func didReceiveAction(notification: NSNotification) { - if let action = notification.object as? Action, indexPath = tableView.indexPathForCell(action.cell) { + if let action = notification.object as? Action, indexPath = tableView?.indexPathForCell(action.cell) { let builder = builderAtIndexPath(indexPath) builder.0.invoke(action: .custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1, userInfo: notification.userInfo) @@ -145,7 +145,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate } public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - + let builder = builderAtIndexPath(indexPath) return builder.0.rowHeight(builder.1) } diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index 3b0e121..e1cb19e 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -27,6 +27,8 @@ public typealias ReturnValue = AnyObject? */ public class TableBaseRowBuilder : RowBuilder { + public private(set) weak var tableDirector: TableDirector? + private var actions = [String: ActionHandler]() private var items = [DataType]() @@ -56,7 +58,7 @@ public class TableBaseRowBuilder CGFloat { - return 0 + return UITableViewAutomaticDimension } // MARK: - Chaining actions - @@ -86,8 +88,8 @@ public class TableBaseRowBuilder CGFloat { @@ -154,8 +170,8 @@ public class TablePrototypeRowBuilder AnyObject? { + + if case .configure = action { + (cell as? CellType)?.configure(items[itemIndex]) + } + return super.invoke(action: action, cell: cell, indexPath: indexPath, itemIndex: itemIndex, userInfo: userInfo) + } + + public override func willUpdateDirector(director: TableDirector?) { + + tableDirector = director + if let tableView = director?.tableView, cell = tableView.dequeueReusableCellWithIdentifier(reusableIdentifier) as? CellType { + prototypeCell = cell + } + } } public func +=(left: TableBaseRowBuilder, right: DataType) { diff --git a/Tablet/TableSectionBuilder.swift b/Tablet/TableSectionBuilder.swift index ac20791..d9e300a 100644 --- a/Tablet/TableSectionBuilder.swift +++ b/Tablet/TableSectionBuilder.swift @@ -29,7 +29,7 @@ public class TableSectionBuilder { weak var tableDirector: TableDirector? { didSet { guard let director = tableDirector else { return } - builders.forEach { $0.registerCell(inTableView: director.tableView) } + builders.forEach { $0.willUpdateDirector(director) } } } @@ -79,7 +79,7 @@ public class TableSectionBuilder { public func append(rows rows: [RowBuilder]) { - if let tableView = tableDirector?.tableView { rows.forEach { $0.registerCell(inTableView: tableView) } } + if let director = tableDirector { rows.forEach { $0.willUpdateDirector(director) } } builders.appendContentsOf(rows) } diff --git a/TabletDemo/Classes/Presentation/Controllers/MainController.swift b/TabletDemo/Classes/Presentation/Controllers/MainController.swift index 1375ff0..8f3faae 100644 --- a/TabletDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TabletDemo/Classes/Presentation/Controllers/MainController.swift @@ -21,7 +21,7 @@ class MainController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let rows = TableRowBuilder(items: ["1", "2", "3"]) + let rows = TablePrototypeRowBuilder(items: ["1"]) .action(.click) { [unowned self] e in self.performSegueWithIdentifier("headerfooter", sender: nil) }