diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 7d51a48..a44748e 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -74,7 +74,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return scrollDelegate?.respondsToSelector(selector) == true ? scrollDelegate : super.forwardingTargetForSelector(selector) } - // MARK: Internal + // MARK: - Internal - func didReceiveAction(notification: NSNotification) { @@ -84,26 +84,23 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate builder.0.invoke(action: .custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1, userInfo: notification.userInfo) } } -} - -public extension TableDirector { - + // MARK: UITableViewDataSource - configuration - func numberOfSectionsInTableView(tableView: UITableView) -> Int { + public func numberOfSectionsInTableView(tableView: UITableView) -> Int { return sections.count } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sections[section].numberOfRowsInSection } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let builder = builderAtIndexPath(indexPath) let cell = tableView.dequeueReusableCellWithIdentifier(builder.0.reusableIdentifier, forIndexPath: indexPath) - + if cell.frame.size.width != tableView.frame.size.width { cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height) cell.layoutIfNeeded() @@ -113,58 +110,47 @@ public extension TableDirector { return cell } -} - -public extension TableDirector { - + // MARK: UITableViewDataSource - section setup - func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sections[section].headerTitle } - func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { + public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { return sections[section].footerTitle } // MARK: UITableViewDelegate - section setup - func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { + public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { return sections[section].headerView } - func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { + public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { return sections[section].footerView } - func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { + public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return sections[section].headerView?.frame.size.height ?? UITableViewAutomaticDimension } - - func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { + + public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return sections[section].footerView?.frame.size.height ?? UITableViewAutomaticDimension } -} - -public extension TableDirector { - + // MARK: UITableViewDelegate - actions - - func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + + public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return CGFloat(builderAtIndexPath(indexPath).0.estimatedRowHeight) } - func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return invoke(action: .height, cell: nil, indexPath: indexPath) as? CGFloat ?? UITableViewAutomaticDimension } - /*func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { + public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - return invokeAction(.willSelect, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? NSIndexPath - }*/ - - func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - let cell = tableView.cellForRowAtIndexPath(indexPath) if invoke(action: .click, cell: cell, indexPath: indexPath) != nil { @@ -174,29 +160,31 @@ public extension TableDirector { } } - func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { invoke(action: .deselect, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) } - func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { invoke(action: .willDisplay, cell: cell, indexPath: indexPath) } - func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool { + public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool { return invoke(action: .shouldHighlight, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? Bool ?? true } -} - -public extension TableDirector { - - // MARK: Sections manipulation - + + /*func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { + + return invokeAction(.willSelect, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? NSIndexPath + }*/ + + // MARK: - Sections manipulation - + public func append(section section: TableSectionBuilder) { append(sections: [section]) } public func append(sections sections: [TableSectionBuilder]) { - + sections.forEach { $0.willMoveToDirector(tableView) } self.sections.appendContentsOf(sections) } diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index 9be3369..a49121a 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -40,28 +40,14 @@ enum ActionHandler { } } -public class RowBuilder : NSObject { +public protocol RowBuilder { - public private(set) var reusableIdentifier: String - public var numberOfRows: Int { - return 0 - } - public var estimatedRowHeight: Float { - return 44 - } + var reusableIdentifier: String { get } + var numberOfRows: Int { get } + var estimatedRowHeight: Float { get } - init(id: String) { - reusableIdentifier = id - } - - // MARK: internal methods, must be overriden in subclass - - func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? { - return nil - } - - func registerCell(inTableView tableView: UITableView) { - } + func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? + func registerCell(inTableView tableView: UITableView) } /** @@ -72,25 +58,32 @@ public class TableBaseRowBuilder]() private var items = [DataType]() - public override var numberOfRows: Int { + public let reusableIdentifier: String + + public var numberOfRows: Int { return items.count } + public var estimatedRowHeight: Float { + return 44 + } + public init(item: DataType, id: String? = nil) { - super.init(id: id ?? String(CellType)) - + + reusableIdentifier = id ?? String(CellType) items.append(item) } public init(items: [DataType]? = nil, id: String? = nil) { - super.init(id: id ?? String(CellType)) - - if items != nil { - self.items.appendContentsOf(items!) + + reusableIdentifier = id ?? String(CellType) + + if let items = items { + self.items.appendContentsOf(items) } } - // MARK: Chaining actions + // MARK: - Chaining actions - public func action(key: String, handler: (data: ActionData) -> Void) -> Self { @@ -99,7 +92,7 @@ public class TableBaseRowBuilder) -> Void) -> Self { - + actions[type.key] = .Handler(handler) return self } @@ -109,10 +102,8 @@ public class TableBaseRowBuilder AnyObject? { + + public func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? { if let action = actions[action.key] { return action.invoke(ActionData(cell: cell as? CellType, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex, userInfo: userInfo)) @@ -120,7 +111,7 @@ public class TableBaseRowBuilder AnyObject? { + public override func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? { switch action { case .configure: diff --git a/Tablet/TableSectionBuilder.swift b/Tablet/TableSectionBuilder.swift index 90cddd7..a011134 100644 --- a/Tablet/TableSectionBuilder.swift +++ b/Tablet/TableSectionBuilder.swift @@ -33,8 +33,8 @@ public class TableSectionBuilder { public var headerTitle: String? public var footerTitle: String? - public var headerView: UIView? - public var footerView: UIView? + public private(set) var headerView: UIView? + public private(set) var footerView: UIView? /// A total number of rows in section of each row builder. public var numberOfRowsInSection: Int { @@ -62,7 +62,7 @@ public class TableSectionBuilder { self.footerView = footerView } - // MARK: Public + // MARK: - Public - public func clear() { builders.removeAll() @@ -78,7 +78,7 @@ public class TableSectionBuilder { builders.appendContentsOf(rows) } - // MARK: Internal + // MARK: - Internal - func builderAtIndex(index: Int) -> (RowBuilder, Int)? { diff --git a/TabletDemo/Classes/Presentation/Controllers/MainController.swift b/TabletDemo/Classes/Presentation/Controllers/MainController.swift index 11e982b..1375ff0 100644 --- a/TabletDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TabletDemo/Classes/Presentation/Controllers/MainController.swift @@ -25,16 +25,6 @@ class MainController: UIViewController { .action(.click) { [unowned self] e in self.performSegueWithIdentifier("headerfooter", sender: nil) } - .valueAction(.click) { data in - return 10 - } - .action(.click) { data in - self.performSegueWithIdentifier("headerfooter", sender: nil) - } - .action(.click) { data in - - - } tableDirector += rows }