height strategy improvements

This commit is contained in:
Max Sokolov 2016-09-06 19:30:47 +03:00
parent 0f93d296ed
commit 29202d4405
3 changed files with 17 additions and 8 deletions

View File

@ -19,7 +19,6 @@ class NibCellsController: UITableViewController {
title = "Nib cells"
tableDirector = TableDirector(tableView: tableView)
//tableDirector.shouldUsePrototypeCellHeightCalculation = true
let numbers = [1000, 2000, 3000, 4000, 5000]

View File

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

View File

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