code improvements
This commit is contained in:
parent
d048613605
commit
4bcd3a1258
Binary file not shown.
|
|
@ -29,7 +29,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
|
|||
public private(set) weak var tableView: UITableView!
|
||||
public weak var scrollDelegate: UIScrollViewDelegate?
|
||||
private var sections = [TableSectionBuilder]()
|
||||
|
||||
|
||||
public init(tableView: UITableView) {
|
||||
super.init()
|
||||
|
||||
|
|
@ -56,24 +56,16 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
|
|||
- Returns: A touple - (builder, builderItemIndex)
|
||||
*/
|
||||
private func builderAtIndexPath(indexPath: NSIndexPath) -> (RowBuilder, Int) {
|
||||
|
||||
return sections[indexPath.section].builderAtIndex(indexPath.row)!
|
||||
}
|
||||
|
||||
// MARK: Public
|
||||
|
||||
public func invokeAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? {
|
||||
|
||||
let builder = builderAtIndexPath(indexPath)
|
||||
return builder.0.invokeAction(action, cell: cell, indexPath: indexPath, itemIndex: builder.1, userInfo: nil)
|
||||
}
|
||||
|
||||
internal func didReceiveAction(notification: NSNotification) {
|
||||
|
||||
if let action = notification.object as? Action, indexPath = tableView.indexPathForCell(action.cell) {
|
||||
|
||||
let builder = builderAtIndexPath(indexPath)
|
||||
builder.0.invokeAction(.custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1, userInfo: notification.userInfo)
|
||||
}
|
||||
}
|
||||
|
||||
public override func respondsToSelector(selector: Selector) -> Bool {
|
||||
return super.respondsToSelector(selector) || scrollDelegate?.respondsToSelector(selector) == true
|
||||
|
|
@ -82,6 +74,17 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
|
|||
public override func forwardingTargetForSelector(selector: Selector) -> AnyObject? {
|
||||
return scrollDelegate?.respondsToSelector(selector) == true ? scrollDelegate : super.forwardingTargetForSelector(selector)
|
||||
}
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
func didReceiveAction(notification: NSNotification) {
|
||||
|
||||
if let action = notification.object as? Action, indexPath = tableView.indexPathForCell(action.cell) {
|
||||
|
||||
let builder = builderAtIndexPath(indexPath)
|
||||
builder.0.invokeAction(.custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1, userInfo: notification.userInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public extension TableDirector {
|
||||
|
|
@ -89,12 +92,10 @@ public extension TableDirector {
|
|||
// MARK: UITableViewDataSource - configuration
|
||||
|
||||
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
||||
|
||||
return sections.count
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
|
||||
return sections[section].numberOfRowsInSection
|
||||
}
|
||||
|
||||
|
|
@ -120,34 +121,28 @@ public extension TableDirector {
|
|||
// MARK: UITableViewDataSource - section setup
|
||||
|
||||
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
||||
|
||||
return sections[section].headerTitle
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
||||
|
||||
return sections[section].footerTitle
|
||||
}
|
||||
|
||||
// MARK: UITableViewDelegate - section setup
|
||||
|
||||
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
||||
|
||||
return sections[section].headerView
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
|
||||
|
||||
return sections[section].footerView
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
||||
|
||||
return sections[section].headerHeight
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
|
||||
|
||||
return sections[section].footerHeight
|
||||
}
|
||||
}
|
||||
|
|
@ -157,12 +152,10 @@ public extension TableDirector {
|
|||
// MARK: UITableViewDelegate - actions
|
||||
|
||||
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
|
||||
|
||||
return CGFloat(builderAtIndexPath(indexPath).0.estimatedRowHeight)
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
|
||||
|
||||
return invokeAction(.height, cell: nil, indexPath: indexPath) as? CGFloat ?? UITableViewAutomaticDimension
|
||||
}
|
||||
|
||||
|
|
@ -183,17 +176,14 @@ public extension TableDirector {
|
|||
}
|
||||
|
||||
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
|
||||
|
||||
invokeAction(.deselect, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath)
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
|
||||
|
||||
invokeAction(.willDisplay, cell: cell, indexPath: indexPath)
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
|
||||
|
||||
return invokeAction(.shouldHighlight, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? Bool ?? true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,8 +109,8 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
|
|||
actions[actionType.key] = .actionReturnBlock(closure)
|
||||
return self
|
||||
}
|
||||
|
||||
// MARK: Triggers
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
override func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? {
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ import Foundation
|
|||
*/
|
||||
public class TableSectionBuilder {
|
||||
|
||||
internal weak var tableView: UITableView?
|
||||
weak var tableView: UITableView?
|
||||
private var builders = [RowBuilder]()
|
||||
|
||||
public var headerTitle: String?
|
||||
|
|
@ -38,10 +38,9 @@ public class TableSectionBuilder {
|
|||
|
||||
public var footerView: UIView?
|
||||
public var footerHeight: CGFloat = UITableViewAutomaticDimension
|
||||
|
||||
|
||||
/// A total number of rows in section of each row builder.
|
||||
public var numberOfRowsInSection: Int {
|
||||
|
||||
return builders.reduce(0) { $0 + $1.numberOfRows }
|
||||
}
|
||||
|
||||
|
|
@ -63,29 +62,8 @@ public class TableSectionBuilder {
|
|||
self.footerView = footerView
|
||||
self.footerHeight = footerHeight
|
||||
}
|
||||
}
|
||||
|
||||
internal extension TableSectionBuilder {
|
||||
|
||||
internal func builderAtIndex(var index: Int) -> (RowBuilder, Int)? {
|
||||
|
||||
for builder in builders {
|
||||
if index < builder.numberOfRows {
|
||||
return (builder, index)
|
||||
}
|
||||
index -= builder.numberOfRows
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
internal func willMoveToDirector(tableView: UITableView) {
|
||||
self.tableView = tableView
|
||||
self.builders.forEach { $0.registerCell(inTableView: tableView) }
|
||||
}
|
||||
}
|
||||
|
||||
public extension TableSectionBuilder {
|
||||
// MARK: Public
|
||||
|
||||
public func clear() {
|
||||
builders.removeAll()
|
||||
|
|
@ -96,10 +74,30 @@ public extension TableSectionBuilder {
|
|||
}
|
||||
|
||||
public func appendRows(rowBuilders: [RowBuilder]) {
|
||||
|
||||
|
||||
if let tableView = tableView { rowBuilders.forEach { $0.registerCell(inTableView: tableView) } }
|
||||
builders.appendContentsOf(rowBuilders)
|
||||
}
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
func builderAtIndex(var index: Int) -> (RowBuilder, Int)? {
|
||||
|
||||
for builder in builders {
|
||||
if index < builder.numberOfRows {
|
||||
return (builder, index)
|
||||
}
|
||||
index -= builder.numberOfRows
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func willMoveToDirector(tableView: UITableView) {
|
||||
|
||||
self.tableView = tableView
|
||||
self.builders.forEach { $0.registerCell(inTableView: tableView) }
|
||||
}
|
||||
}
|
||||
|
||||
public func +=(left: TableSectionBuilder, right: RowBuilder) {
|
||||
|
|
|
|||
|
|
@ -92,7 +92,6 @@ public class Action {
|
|||
}
|
||||
|
||||
public func invoke() {
|
||||
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(TabletNotifications.CellAction, object: self, userInfo: userInfo)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue