From 7faee8f24e0f3fd743e4628b87a77914d8ded93c Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Mon, 6 Jun 2016 23:35:20 +0300 Subject: [PATCH] add HeightStrategies --- Tablet/HeightStrategy.swift | 66 +++++++++++++++++++ Tablet/TableDirector.swift | 22 ++++--- Tablet/Tablet.xcodeproj/project.pbxproj | 4 ++ .../Controllers/MainController.swift | 11 +++- 4 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 Tablet/HeightStrategy.swift diff --git a/Tablet/HeightStrategy.swift b/Tablet/HeightStrategy.swift new file mode 100644 index 0000000..4938e66 --- /dev/null +++ b/Tablet/HeightStrategy.swift @@ -0,0 +1,66 @@ +// +// Copyright (c) 2015 Max Sokolov https://twitter.com/max_sokolov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +import UIKit + +public protocol HeightStrategy { + + func height(item: Item, cell: Cell.Type) -> CGFloat + + func estimatedHeight() -> CGFloat +} + +public class AutolayoutHeightStrategy: HeightStrategy { + + public func height(item: Item, cell: Cell.Type) -> CGFloat { + return UITableViewAutomaticDimension + } + + public func estimatedHeight() -> CGFloat { + return 0 + } +} + +public class PrototypeHeightStrategy: HeightStrategy { + + private weak var tableView: UITableView? + + public init(tableView: UITableView) { + self.tableView = tableView + } + + public func height(item: Item, cell: Cell.Type) -> CGFloat { + + guard let cell = tableView?.dequeueReusableCellWithIdentifier(Cell.reusableIdentifier()) as? Cell else { return 0 } + + cell.bounds = CGRectMake(0, 0, tableView?.bounds.size.width ?? 0, cell.bounds.height) + + cell.configure(item) + + cell.setNeedsLayout() + cell.layoutIfNeeded() + + return cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height + 1 + } + + public func estimatedHeight() -> CGFloat { + return UITableViewAutomaticDimension + } +} \ No newline at end of file diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 7860e71..4ed254f 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -86,6 +86,18 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate } } + // MARK: - Height + + public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + let builder = builderAtIndexPath(indexPath) + return builder.0.estimatedRowHeight(builder.1) + } + + public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + let builder = builderAtIndexPath(indexPath) + return builder.0.rowHeight(builder.1) + } + // MARK: UITableViewDataSource - configuration public func numberOfSectionsInTableView(tableView: UITableView) -> Int { @@ -144,16 +156,6 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate // MARK: UITableViewDelegate - actions - public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - let builder = builderAtIndexPath(indexPath) - return builder.0.estimatedRowHeight(builder.1) - } - - public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - let builder = builderAtIndexPath(indexPath) - return builder.0.rowHeight(builder.1) - } - public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) diff --git a/Tablet/Tablet.xcodeproj/project.pbxproj b/Tablet/Tablet.xcodeproj/project.pbxproj index 84e3ea4..ea1054c 100644 --- a/Tablet/Tablet.xcodeproj/project.pbxproj +++ b/Tablet/Tablet.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ DA08A0511CF3AB6100BBF1F8 /* RowBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA08A0501CF3AB6100BBF1F8 /* RowBuilder.swift */; }; DA539C9F1CFB025C00368ACB /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA539C9E1CFB025C00368ACB /* Operators.swift */; }; DA539CC61D01D44500368ACB /* TableDynamicRowBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA539CC51D01D44500368ACB /* TableDynamicRowBuilder.swift */; }; + DA9EA7351D06155E0021F650 /* HeightStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA7341D06155E0021F650 /* HeightStrategy.swift */; }; DAC2D6741C9D743D009E9C19 /* Tablet.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DAC2D6691C9D743D009E9C19 /* Tablet.framework */; }; DAC2D6871C9D7517009E9C19 /* Tablet.h in Headers */ = {isa = PBXBuildFile; fileRef = DAC2D6851C9D7517009E9C19 /* Tablet.h */; settings = {ATTRIBUTES = (Public, ); }; }; DAC2D6901C9D799E009E9C19 /* TableDirector.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC2D68C1C9D799E009E9C19 /* TableDirector.swift */; }; @@ -39,6 +40,7 @@ DA08A0501CF3AB6100BBF1F8 /* RowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowBuilder.swift; sourceTree = ""; }; DA539C9E1CFB025C00368ACB /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = ""; }; DA539CC51D01D44500368ACB /* TableDynamicRowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableDynamicRowBuilder.swift; sourceTree = ""; }; + DA9EA7341D06155E0021F650 /* HeightStrategy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeightStrategy.swift; sourceTree = ""; }; DAC2D6691C9D743D009E9C19 /* Tablet.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Tablet.framework; sourceTree = BUILT_PRODUCTS_DIR; }; DAC2D6731C9D743D009E9C19 /* TabletTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TabletTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; DAC2D6841C9D7517009E9C19 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -111,6 +113,7 @@ DA08A04E1CF3AB0C00BBF1F8 /* ConfigurableCell.swift */, DAC2D68F1C9D799E009E9C19 /* Tablet.swift */, DA539C9E1CFB025C00368ACB /* Operators.swift */, + DA9EA7341D06155E0021F650 /* HeightStrategy.swift */, ); name = Classes; sourceTree = ""; @@ -235,6 +238,7 @@ files = ( DAC2D6901C9D799E009E9C19 /* TableDirector.swift in Sources */, DAC2D6921C9D799E009E9C19 /* TableSectionBuilder.swift in Sources */, + DA9EA7351D06155E0021F650 /* HeightStrategy.swift in Sources */, DA08A0511CF3AB6100BBF1F8 /* RowBuilder.swift in Sources */, DA539CC61D01D44500368ACB /* TableDynamicRowBuilder.swift in Sources */, DA08A04F1CF3AB0C00BBF1F8 /* ConfigurableCell.swift in Sources */, diff --git a/TabletDemo/Classes/Presentation/Controllers/MainController.swift b/TabletDemo/Classes/Presentation/Controllers/MainController.swift index 9bf5755..17d4c78 100644 --- a/TabletDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TabletDemo/Classes/Presentation/Controllers/MainController.swift @@ -9,10 +9,12 @@ import UIKit import Tablet -class TableUpdateTask { - -} + + + + + @@ -28,6 +30,9 @@ class MainController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + let h = PrototypeHeightStrategy(tableView: tableView) + h.height("123", cell: StoryboardImageTableViewCell.self) + let rowBuilder = TableRowBuilder(items: ["1", "1", "1", "1"]) .action(.click) { [unowned self] e in self.performSegueWithIdentifier("headerfooter", sender: nil)