add HeightStrategies

This commit is contained in:
Max Sokolov 2016-06-06 23:35:20 +03:00
parent e121c3fbc6
commit 7faee8f24e
4 changed files with 90 additions and 13 deletions

View File

@ -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: Hashable, Cell: ConfigurableCell where Cell.T == Item, Cell: UITableViewCell>(item: Item, cell: Cell.Type) -> CGFloat
func estimatedHeight() -> CGFloat
}
public class AutolayoutHeightStrategy: HeightStrategy {
public func height<Item, Cell: ConfigurableCell where Cell.T == Item, Cell: UITableViewCell>(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: Hashable, Cell: ConfigurableCell where Cell.T == Item, Cell: UITableViewCell>(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
}
}

View File

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

View File

@ -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 = "<group>"; };
DA539C9E1CFB025C00368ACB /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
DA539CC51D01D44500368ACB /* TableDynamicRowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableDynamicRowBuilder.swift; sourceTree = "<group>"; };
DA9EA7341D06155E0021F650 /* HeightStrategy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeightStrategy.swift; sourceTree = "<group>"; };
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 = "<group>"; };
@ -111,6 +113,7 @@
DA08A04E1CF3AB0C00BBF1F8 /* ConfigurableCell.swift */,
DAC2D68F1C9D799E009E9C19 /* Tablet.swift */,
DA539C9E1CFB025C00368ACB /* Operators.swift */,
DA9EA7341D06155E0021F650 /* HeightStrategy.swift */,
);
name = Classes;
sourceTree = "<group>";
@ -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 */,

View File

@ -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<String, StoryboardImageTableViewCell>(items: ["1", "1", "1", "1"])
.action(.click) { [unowned self] e in
self.performSegueWithIdentifier("headerfooter", sender: nil)