attempt to auto register cells

This commit is contained in:
Max Sokolov 2015-12-06 21:37:19 +03:00
parent f7c80a3dd1
commit 28a354873c
6 changed files with 50 additions and 36 deletions

View File

@ -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]))

View File

@ -102,6 +102,22 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : 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 +=<T, C where C : UITableViewCell>(var left: TableRowBuilder<T, C>, right: T) -> TableRowBuilder<T, C> {
left.appendItems([right])
return left
}

View File

@ -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)
}
}

View File

@ -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?
}

View File

@ -18,7 +18,7 @@ class ViewController: UIViewController {
tableDirector = TableDirector(tableView: tableView)
var rowBuilder = TableRowBuilder<Int, UITableViewCell>(items: [1, 2, 3, 4], id: "cell")
let rowBuilder = TableRowBuilder<Int, UITableViewCell>(items: [1, 2, 3, 4], id: "cell")
.action(.configure) { data in
data.cell?.textLabel?.text = "\(data.item)"