diff --git a/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift b/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift index 618e02a..9340070 100644 --- a/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift +++ b/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift @@ -14,6 +14,7 @@ class AutolayoutCellsController: UIViewController { @IBOutlet weak var tableView: UITableView! { didSet { tableDirector = TableDirector(tableView: tableView) + tableDirector.shouldUsePrototypeCellHeightCalculation = true } } var tableDirector: TableDirector! @@ -26,7 +27,7 @@ class AutolayoutCellsController: UIViewController { let section = TableSection() var rows = 0 - while rows <= 10 { + while rows <= 1000 { rows += 1 let row = TableRow(item: ()) diff --git a/Demo/Classes/Presentation/Controllers/MainController.swift b/Demo/Classes/Presentation/Controllers/MainController.swift index b57a0db..9c1534f 100644 --- a/Demo/Classes/Presentation/Controllers/MainController.swift +++ b/Demo/Classes/Presentation/Controllers/MainController.swift @@ -35,6 +35,8 @@ class MainController: UIViewController { } } + tableView.registerClass(ConfigurableTableViewCell.self, forCellReuseIdentifier: "ConfigurableTableViewCell") + let rows: [Row] = [ TableRow(item: "Autolayout cells", actions: [clickAction]), diff --git a/Demo/Classes/Presentation/Controllers/NibCellsController.swift b/Demo/Classes/Presentation/Controllers/NibCellsController.swift index 5309ebf..1b2f412 100644 --- a/Demo/Classes/Presentation/Controllers/NibCellsController.swift +++ b/Demo/Classes/Presentation/Controllers/NibCellsController.swift @@ -19,6 +19,7 @@ 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 ec505d6..491a949 100644 --- a/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift +++ b/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift @@ -20,7 +20,7 @@ class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { @IBOutlet var subtitleLabel: UILabel! static var estimatedHeight: CGFloat? { - return 500 + return 700 } func configure(with string: T) { @@ -28,4 +28,11 @@ class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { titleLabel.text = LoremIpsumTitle subtitleLabel.text = LoremIpsumBody } + + override func layoutSubviews() { + super.layoutSubviews() + + titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.size.width + subtitleLabel.preferredMaxLayoutWidth = subtitleLabel.bounds.size.width + } } \ No newline at end of file diff --git a/Demo/Classes/Presentation/Views/NibTableViewCell.xib b/Demo/Classes/Presentation/Views/NibTableViewCell.xib index 1b9ae31..34c76d1 100644 --- a/Demo/Classes/Presentation/Views/NibTableViewCell.xib +++ b/Demo/Classes/Presentation/Views/NibTableViewCell.xib @@ -1,5 +1,5 @@ - + @@ -11,19 +11,21 @@ - + - - + + + + diff --git a/Sources/HeightStrategy.swift b/Sources/HeightStrategy.swift index f0f778d..2aa4800 100644 --- a/Sources/HeightStrategy.swift +++ b/Sources/HeightStrategy.swift @@ -59,7 +59,7 @@ 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.setNeedsLayout() diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 3cf11b6..88c90ea 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -98,14 +98,18 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let row = sections[indexPath.section].rows[indexPath.row] + + cellRegisterer?.register(cellType: row.cellType, forCellReuseIdentifier: row.reuseIdentifier) + return row.estimatedHeight ?? heightStrategy?.estimatedHeight(row, path: indexPath) ?? UITableViewAutomaticDimension } public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let row = sections[indexPath.section].rows[indexPath.row] + let rowHeight = invoke(action: .height, cell: nil, indexPath: indexPath) as? CGFloat - + return rowHeight ?? row.defaultHeight ?? heightStrategy?.height(row, path: indexPath) ?? UITableViewAutomaticDimension } @@ -122,9 +126,6 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let row = sections[indexPath.section].rows[indexPath.row] - - cellRegisterer?.register(cellType: row.cellType, forCellReuseIdentifier: row.reuseIdentifier) - let cell = tableView.dequeueReusableCellWithIdentifier(row.reuseIdentifier, forIndexPath: indexPath) if cell.frame.size.width != tableView.frame.size.width {