diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 2e8ed72..8f2f74e 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -35,7 +35,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate self.tableView = tableView self.tableView.delegate = self self.tableView.dataSource = self - + NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveAction:", name: kActionPerformedNotificationKey, object: nil) } @@ -43,8 +43,8 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate NSNotificationCenter.defaultCenter().removeObserver(self) } - - // MARK: Public methods + + // MARK: Sections manipulation public func appendSection(section: TableSectionBuilder) { sections.append(section) @@ -69,7 +69,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return sections[indexPath.section].builderAtIndex(indexPath.row)! } - private func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? { + public func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? { let builder = builderAtIndexPath(indexPath) return builder.0.triggerAction(action, cell: cell, indexPath: indexPath, itemIndex: builder.1) @@ -103,10 +103,13 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate let cell = tableView.dequeueReusableCellWithIdentifier(builder.0.reusableIdentifier, forIndexPath: indexPath) builder.0.triggerAction(.configure, cell: cell, indexPath: indexPath, itemIndex: builder.1) - + return cell } - +} + +extension TableDirector { + // MARK: UITableViewDataSource - section setup public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { @@ -140,11 +143,14 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return sections[section].footerHeight } +} + +extension TableDirector { // MARK: UITableViewDelegate - actions public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - + let cell = tableView.cellForRowAtIndexPath(indexPath) if triggerAction(.click, cell: cell, indexPath: indexPath) != nil { @@ -169,8 +175,14 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate return triggerAction(.shouldHighlight, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? Bool ?? true } + public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + + return 300 + } + public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - return triggerAction(.height, cell: nil, indexPath: indexPath) as? CGFloat ?? tableView.rowHeight + print(indexPath) + return triggerAction(.height, cell: nil, indexPath: indexPath) as? CGFloat ?? UITableViewAutomaticDimension } } \ No newline at end of file diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index a7f2044..c3cf2fd 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -21,12 +21,14 @@ import UIKit import Foundation +public typealias ReturnValue = AnyObject? + internal enum ActionHandler { case actionBlock((data: ActionData) -> Void) - case actionReturnBlock((data: ActionData) -> AnyObject) + case actionReturnBlock((data: ActionData) -> AnyObject?) - func call(data: ActionData) -> AnyObject { + func call(data: ActionData) -> AnyObject? { switch (self) { case .actionBlock(let closure): @@ -45,23 +47,26 @@ public class TableRowBuilder : RowBuilder { private var actions = Dictionary>() private var items = [I]() - + public var reusableIdentifier: String + public var estimatedRowHeight: Float public var numberOfRows: Int { get { return items.count } } - public init(item: I, id: String) { + public init(item: I, id: String, estimatedRowHeight: Float = 0) { reusableIdentifier = id + self.estimatedRowHeight = estimatedRowHeight items.append(item) } - public init(items: [I]? = nil, id: String) { - + public init(items: [I]? = nil, id: String, estimatedRowHeight: Float = 0) { + reusableIdentifier = id + self.estimatedRowHeight = estimatedRowHeight if items != nil { self.items.appendContentsOf(items!) @@ -94,7 +99,7 @@ public class TableRowBuilder : RowBuilder { return self } - public func action(actionType: ActionType, closure: (data: ActionData) -> AnyObject) -> Self { + public func action(actionType: ActionType, closure: (data: ActionData) -> ReturnValue) -> Self { actions[actionType.key] = .actionReturnBlock(closure) return self @@ -116,12 +121,12 @@ public class TableRowBuilder : RowBuilder { */ public class TableConfigurableRowBuilder : TableRowBuilder { - public init(item: I) { - super.init(item: item, id: C.reusableIdentifier()) + public init(item: I, estimatedRowHeight: Float = 0) { + super.init(item: item, id: C.reusableIdentifier(), estimatedRowHeight: estimatedRowHeight) } - - public init(items: [I]? = nil) { - super.init(items: items, id: C.reusableIdentifier()) + + public init(items: [I]? = nil, estimatedRowHeight: Float = 0) { + super.init(items: items, id: C.reusableIdentifier(), estimatedRowHeight: estimatedRowHeight) } public override func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? { diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index 5c6f0ee..64cfafa 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -40,8 +40,8 @@ public enum ActionType { var key: String { switch (self) { - case .custom(let str): - return str + case .custom(let key): + return key default: return "_\(self)" } @@ -112,6 +112,7 @@ public protocol RowBuilder { var numberOfRows: Int { get } var reusableIdentifier: String { get } + var estimatedRowHeight: Float { get } func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? } \ No newline at end of file diff --git a/TabletDemo/TabletDemo.xcodeproj/project.pbxproj b/TabletDemo/TabletDemo.xcodeproj/project.pbxproj index 42969fb..3a5cbf8 100644 --- a/TabletDemo/TabletDemo.xcodeproj/project.pbxproj +++ b/TabletDemo/TabletDemo.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 508B71841BF48DD300272920 /* TableSectionBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508B71831BF48DD300272920 /* TableSectionBuilder.swift */; }; 508B71861BF48E0D00272920 /* TableRowBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508B71851BF48E0D00272920 /* TableRowBuilder.swift */; }; DA1BCD0F1BF5472C00CC0479 /* TableDirector.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA1BCD0E1BF5472C00CC0479 /* TableDirector.swift */; }; + DA1BCD111BF7388C00CC0479 /* CustomTableActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA1BCD101BF7388C00CC0479 /* CustomTableActions.swift */; }; DAB7EB2B1BEF787300D2AD5E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAB7EB2A1BEF787300D2AD5E /* AppDelegate.swift */; }; DAB7EB2D1BEF787300D2AD5E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAB7EB2C1BEF787300D2AD5E /* ViewController.swift */; }; DAB7EB301BEF787300D2AD5E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAB7EB2E1BEF787300D2AD5E /* Main.storyboard */; }; @@ -23,6 +24,7 @@ 508B71831BF48DD300272920 /* TableSectionBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableSectionBuilder.swift; sourceTree = ""; }; 508B71851BF48E0D00272920 /* TableRowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRowBuilder.swift; sourceTree = ""; }; DA1BCD0E1BF5472C00CC0479 /* TableDirector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableDirector.swift; sourceTree = ""; }; + DA1BCD101BF7388C00CC0479 /* CustomTableActions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTableActions.swift; sourceTree = ""; }; DAB7EB271BEF787300D2AD5E /* TabletDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TabletDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; DAB7EB2A1BEF787300D2AD5E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; DAB7EB2C1BEF787300D2AD5E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -68,6 +70,7 @@ DAB7EB2A1BEF787300D2AD5E /* AppDelegate.swift */, DAB7EB2C1BEF787300D2AD5E /* ViewController.swift */, DAB7EB3F1BEFD07E00D2AD5E /* ConfigurableTableViewCell.swift */, + DA1BCD101BF7388C00CC0479 /* CustomTableActions.swift */, DAB7EB2E1BEF787300D2AD5E /* Main.storyboard */, DAB7EB311BEF787300D2AD5E /* Assets.xcassets */, DAB7EB331BEF787300D2AD5E /* LaunchScreen.storyboard */, @@ -165,6 +168,7 @@ DA1BCD0F1BF5472C00CC0479 /* TableDirector.swift in Sources */, DAB7EB401BEFD07E00D2AD5E /* ConfigurableTableViewCell.swift in Sources */, DAB7EB2B1BEF787300D2AD5E /* AppDelegate.swift in Sources */, + DA1BCD111BF7388C00CC0479 /* CustomTableActions.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 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 3ddca07..6c57e1d 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/Base.lproj/Main.storyboard b/TabletDemo/TabletDemo/Base.lproj/Main.storyboard index 98f93de..2bd0123 100644 --- a/TabletDemo/TabletDemo/Base.lproj/Main.storyboard +++ b/TabletDemo/TabletDemo/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -49,24 +49,41 @@ - + - - + + + + + + + diff --git a/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift b/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift index 3d96de5..a83405f 100644 --- a/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift +++ b/TabletDemo/TabletDemo/ConfigurableTableViewCell.swift @@ -15,6 +15,7 @@ class ConfigurableTableViewCell: UITableViewCell, ConfigurableCell { typealias Item = String @IBOutlet weak var button: UIButton! + @IBOutlet weak var contentLabel: UILabel! static func reusableIdentifier() -> String { diff --git a/TabletDemo/TabletDemo/CustomTableActions.swift b/TabletDemo/TabletDemo/CustomTableActions.swift new file mode 100644 index 0000000..3c55cd9 --- /dev/null +++ b/TabletDemo/TabletDemo/CustomTableActions.swift @@ -0,0 +1,10 @@ +// +// CustomTableActions.swift +// TabletDemo +// +// Created by Max Sokolov on 14/11/15. +// Copyright © 2015 Tablet. All rights reserved. +// + +import UIKit +import Foundation diff --git a/TabletDemo/TabletDemo/ViewController.swift b/TabletDemo/TabletDemo/ViewController.swift index fd74961..9b0aa9b 100644 --- a/TabletDemo/TabletDemo/ViewController.swift +++ b/TabletDemo/TabletDemo/ViewController.swift @@ -23,29 +23,39 @@ class ViewController: UIViewController { data.cell?.textLabel?.text = "\(data.item)" } - .action(.height) { data in - - return 50 - } .action(.shouldHighlight) { data in return false } - let configurableRowBuilder = TableConfigurableRowBuilder(items: ["5", "6", "7", "8"]) + let configurableRowBuilder = TableConfigurableRowBuilder(items: ["5", "6", "7", "8"], estimatedRowHeight: 300) .action(.click) { data -> Void in - - data.cell!.textLabel?.text = "" - + print("click action indexPath: \(data.indexPath), item: \(data.item)") } - .action(kConfigurableTableViewCellButtonClickedAction) { data in + .action(kConfigurableTableViewCellButtonClickedAction) { data -> Void in print("custom action indexPath: \(data.indexPath), item: \(data.item)") } - - let sectionBuilder = TableSectionBuilder(headerTitle: "Tablet", footerTitle: "Deal with table view like a boss.", rowBuilders: [rowBuilder, configurableRowBuilder]) + .action(.height) { data -> ReturnValue in + + if data.item == "5" { + return 70 + } + return nil + } + .action(.configure) { (data) -> Void in + + data.cell!.contentLabel.text = "With iOS 8, Apple has internalized much of the work that previously had to be implemented by you prior to iOS 8. In order to allow the self-sizing cell mechanism to work, you must first set the rowHeight property on the table view to the constant UITableViewAutomaticDimension. Then, you simply need to enable row height estimation by setting the table view's estimatedRowHeight property to a nonzero value, for example" + + //data.cell!.setNeedsUpdateConstraints() + //data.cell!.updateConstraintsIfNeeded() + } + + let sectionBuilder = TableSectionBuilder(headerTitle: "Tablet", footerTitle: "Deal with table view like a boss.", rowBuilders: [configurableRowBuilder]) tableDirector.appendSection(sectionBuilder) } -} \ No newline at end of file +} + +// вход со стороны кутузовского проспекта между домами 10 14 левее чайхоны, на охране сказать кодовое слово Магия - найти клуб лабиринт \ No newline at end of file