diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 9856c1c..087e953 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -73,33 +73,6 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate builder.0.invokeAction(.custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1, userInfo: action.userInfo) } } - - func registerNibs() { - - let bundle = NSBundle(forClass: UITableViewCell.self) - - if let _ = bundle.pathForResource("cell name", ofType: "nib") { // existing cell - - tableView.registerNib(UINib(nibName: "cell", bundle: bundle), forCellReuseIdentifier: "cell id") - - } else { - - tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "") - } - } -} - -extension TableDirector { - - // MARK: Sections manipulation - - public func appendSection(section: TableSectionBuilder) { - sections.append(section) - } - - public func appendSections(sections: [TableSectionBuilder]) { - self.sections.appendContentsOf(sections) - } } public extension TableDirector { @@ -206,6 +179,21 @@ public extension TableDirector { } } +extension TableDirector { + + // MARK: Sections manipulation + + public func appendSection(section: TableSectionBuilder) { + appendSections([section]) + } + + public func appendSections(sections: [TableSectionBuilder]) { + + sections.forEach { $0.willMoveToDirector(tableView) } + self.sections.appendContentsOf(sections) + } +} + public func +=(left: TableDirector, right: RowBuilder) { left.appendSection(TableSectionBuilder(rowBuilders: [right])) diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index d980ac1..92649ea 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -102,6 +102,22 @@ public class TableRowBuilder : RowBuilder { } return nil } + + public func registerCell(inTableView tableView: UITableView) { + + guard let resource = NSStringFromClass(C).componentsSeparatedByString(".").last else { return } + + let bundle = NSBundle(forClass: C.self) + + if let _ = bundle.pathForResource(resource, ofType: "nib") { // existing cell + + tableView.registerNib(UINib(nibName: resource, bundle: bundle), forCellReuseIdentifier: reusableIdentifier) + + } else { + + tableView.registerClass(C.self, forCellReuseIdentifier: reusableIdentifier) + } + } } /** @@ -141,10 +157,4 @@ public extension TableRowBuilder { items.removeAll() } -} - -public func +=(var left: TableRowBuilder, right: T) -> TableRowBuilder { - - left.appendItems([right]) - return left } \ No newline at end of file diff --git a/Tablet/TableSectionBuilder.swift b/Tablet/TableSectionBuilder.swift index f74bdd3..2fcfd16 100644 --- a/Tablet/TableSectionBuilder.swift +++ b/Tablet/TableSectionBuilder.swift @@ -27,6 +27,7 @@ import Foundation */ public class TableSectionBuilder { + internal weak var tableView: UITableView? private var builders = [RowBuilder]() public var headerTitle: String? @@ -45,7 +46,7 @@ public class TableSectionBuilder { } public init(headerTitle: String? = nil, footerTitle: String? = nil, rowBuilders: [RowBuilder]? = nil) { - + self.headerTitle = headerTitle self.footerTitle = footerTitle @@ -62,6 +63,9 @@ public class TableSectionBuilder { self.footerView = footerView self.footerHeight = footerHeight } +} + +internal extension TableSectionBuilder { internal func builderAtIndex(var index: Int) -> (RowBuilder, Int)? { @@ -74,6 +78,11 @@ public class TableSectionBuilder { return nil } + + internal func willMoveToDirector(tableView: UITableView) { + self.tableView = tableView + self.builders.forEach { $0.registerCell(inTableView: tableView) } + } } public extension TableSectionBuilder { @@ -87,6 +96,12 @@ public extension TableSectionBuilder { public func appendRowBuilder(rowBuilder: RowBuilder) { - builders.append(rowBuilder) + appendRowBuilders([rowBuilder]) + } + + public func appendRowBuilders(rowBuilders: [RowBuilder]) { + + if let tableView = tableView { rowBuilders.forEach { $0.registerCell(inTableView: tableView) } } + builders.appendContentsOf(rowBuilders) } } \ No newline at end of file diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index 12158ed..8b2c35e 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -122,5 +122,6 @@ public protocol RowBuilder { var reusableIdentifier: String { get } var estimatedRowHeight: CGFloat { get } + func registerCell(inTableView tableView: UITableView) func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? } \ No newline at end of file diff --git a/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate b/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate index 5dccf4a..5bee61d 100644 Binary files a/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate and b/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TabletDemo/TabletDemo/ViewController.swift b/TabletDemo/TabletDemo/ViewController.swift index bb14c76..9d996c0 100644 --- a/TabletDemo/TabletDemo/ViewController.swift +++ b/TabletDemo/TabletDemo/ViewController.swift @@ -18,7 +18,7 @@ class ViewController: UIViewController { tableDirector = TableDirector(tableView: tableView) - var rowBuilder = TableRowBuilder(items: [1, 2, 3, 4], id: "cell") + let rowBuilder = TableRowBuilder(items: [1, 2, 3, 4], id: "cell") .action(.configure) { data in data.cell?.textLabel?.text = "\(data.item)"