From 29202d4405a1607cfdad4f74cd2cccccb691c640 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Tue, 6 Sep 2016 19:30:47 +0300 Subject: [PATCH] height strategy improvements --- .../Controllers/NibCellsController.swift | 1 - .../Views/AutolayoutTableViewCell.swift | 4 +++- Sources/HeightStrategy.swift | 20 +++++++++++++------ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Demo/Classes/Presentation/Controllers/NibCellsController.swift b/Demo/Classes/Presentation/Controllers/NibCellsController.swift index 1b2f412..5309ebf 100644 --- a/Demo/Classes/Presentation/Controllers/NibCellsController.swift +++ b/Demo/Classes/Presentation/Controllers/NibCellsController.swift @@ -19,7 +19,6 @@ class NibCellsController: UITableViewController { title = "Nib cells" tableDirector = TableDirector(tableView: tableView) - //tableDirector.shouldUsePrototypeCellHeightCalculation = true let numbers = [1000, 2000, 3000, 4000, 5000] diff --git a/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift b/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift index 491a949..ff4219e 100644 --- a/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift +++ b/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift @@ -18,7 +18,7 @@ class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { @IBOutlet var titleLabel: UILabel! @IBOutlet var subtitleLabel: UILabel! - + static var estimatedHeight: CGFloat? { return 700 } @@ -32,6 +32,8 @@ class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { override func layoutSubviews() { super.layoutSubviews() + contentView.layoutIfNeeded() + titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.size.width subtitleLabel.preferredMaxLayoutWidth = subtitleLabel.bounds.size.width } diff --git a/Sources/HeightStrategy.swift b/Sources/HeightStrategy.swift index 2aa4800..66b715b 100644 --- a/Sources/HeightStrategy.swift +++ b/Sources/HeightStrategy.swift @@ -57,11 +57,10 @@ public class PrototypeHeightStrategy: CellHeightCalculatable { } guard let cell = prototypeCell else { return 0 } - - cell.bounds = CGRectMake(0, 0, tableView.bounds.size.width, cell.bounds.height) row.configure(cell) - + + cell.bounds = CGRectMake(0, 0, tableView.bounds.size.width, cell.bounds.height) cell.setNeedsLayout() cell.layoutIfNeeded() @@ -74,10 +73,19 @@ public class PrototypeHeightStrategy: CellHeightCalculatable { public func estimatedHeight(row: Row, path: NSIndexPath) -> CGFloat { - if let estimatedHeight = row.estimatedHeight where estimatedHeight > 0 { - return estimatedHeight + guard let tableView = tableView else { return 0 } + + let hash = row.hashValue ^ Int(tableView.bounds.size.width).hashValue + + if let height = cachedHeights[hash] { + return height } - return height(row, path: path) + + if let estimatedHeight = row.estimatedHeight where estimatedHeight > 0 { + return estimatedHeight + } + + return UITableViewAutomaticDimension } public func invalidate() {