move back to RowBuilder protocol

This commit is contained in:
Max Sokolov 2016-05-08 00:39:58 +03:00
parent c3fda15cb4
commit 87e7dbd6c4
4 changed files with 64 additions and 98 deletions

View File

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

View File

@ -40,28 +40,14 @@ enum ActionHandler<DataType, CellType> {
}
}
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<DataType, CellType where CellType: UITableViewC
private var actions = [String: ActionHandler<DataType, CellType>]()
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<DataType, CellType>) -> Void) -> Self {
@ -99,7 +92,7 @@ public class TableBaseRowBuilder<DataType, CellType where CellType: UITableViewC
}
public func action(type: ActionType, handler: (data: ActionData<DataType, CellType>) -> Void) -> Self {
actions[type.key] = .Handler(handler)
return self
}
@ -109,10 +102,8 @@ public class TableBaseRowBuilder<DataType, CellType where CellType: UITableViewC
actions[type.key] = .ValueHandler(handler)
return self
}
// MARK: Internal
override func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> 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<DataType, CellType where CellType: UITableViewC
return nil
}
override func registerCell(inTableView tableView: UITableView) {
public func registerCell(inTableView tableView: UITableView) {
if tableView.dequeueReusableCellWithIdentifier(reusableIdentifier) != nil {
return
@ -130,16 +121,13 @@ public class TableBaseRowBuilder<DataType, CellType where CellType: UITableViewC
let bundle = NSBundle(forClass: CellType.self)
if let _ = bundle.pathForResource(resource, ofType: "nib") { // existing cell
tableView.registerNib(UINib(nibName: resource, bundle: bundle), forCellReuseIdentifier: reusableIdentifier)
} else {
tableView.registerClass(CellType.self, forCellReuseIdentifier: reusableIdentifier)
}
}
// MARK: Items manipulation
// MARK: - Items manipulation -
public func append(items items: [DataType]) {
self.items.appendContentsOf(items)
@ -158,16 +146,16 @@ public class TableRowBuilder<DataType, CellType: ConfigurableCell where CellType
public override var estimatedRowHeight: Float {
return CellType.estimatedHeight()
}
public init(item: DataType) {
super.init(item: item, id: CellType.reusableIdentifier())
}
public init(items: [DataType]? = nil) {
super.init(items: items, id: CellType.reusableIdentifier())
}
override func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? {
public override func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? {
switch action {
case .configure:

View File

@ -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)? {

View File

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