From cc75e84f5839697a264c679a22b4352bda9a6675 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Tue, 14 Jun 2016 20:47:39 +0300 Subject: [PATCH] try height strategy --- Sources/HeightStrategy.swift | 12 ++++++------ Sources/TableDirector.swift | 8 +++++++- Sources/TableRow.swift | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Sources/HeightStrategy.swift b/Sources/HeightStrategy.swift index c34bd40..9072767 100644 --- a/Sources/HeightStrategy.swift +++ b/Sources/HeightStrategy.swift @@ -20,25 +20,25 @@ import UIKit -public protocol HeightStrategy { +public protocol HeightCalculatingStrategy { var tableView: UITableView? { get set } - func height(item: Item, indexPath: NSIndexPath, cell: Cell.Type) -> CGFloat + func height(indexPath: NSIndexPath, reusableIdentifier: String, configure: (cell: UITableViewCell) -> Void) -> CGFloat } -public class PrototypeHeightStrategy: HeightStrategy { +public class PrototypeHeightStrategy { public weak var tableView: UITableView? private var cachedHeights = [Int: CGFloat]() - public func height(item: Item, indexPath: NSIndexPath, cell: Cell.Type) -> CGFloat { + public func height(indexPath: NSIndexPath, reusableIdentifier: String, configure: (cell: UITableViewCell) -> Void) -> CGFloat { - guard let cell = tableView?.dequeueReusableCellWithIdentifier(Cell.reusableIdentifier()) as? Cell else { return 0 } + guard let cell = tableView?.dequeueReusableCellWithIdentifier(reusableIdentifier) else { return 0 } cell.bounds = CGRectMake(0, 0, tableView?.bounds.size.width ?? 0, cell.bounds.height) - cell.configure(item) + configure(cell: cell) cell.setNeedsLayout() cell.layoutIfNeeded() diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 03545f6..3e8075f 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -28,6 +28,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public private(set) weak var tableView: UITableView? public private(set) var sections = [TableSection]() private weak var scrollDelegate: UIScrollViewDelegate? + private var heightStrategy: HeightCalculatingStrategy? public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil) { super.init() @@ -81,7 +82,12 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate } public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - return sections[indexPath.section].items[indexPath.row].defaultHeight + + let row = sections[indexPath.section].items[indexPath.row] + + return heightStrategy?.height(indexPath, reusableIdentifier: row.reusableIdentifier, configure: { (cell) in + row.configure(cell) + }) ?? sections[indexPath.section].items[indexPath.row].defaultHeight } // MARK: UITableViewDataSource - configuration diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 2b06615..62fd90b 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -60,7 +60,7 @@ public class TableRow