try height strategy

This commit is contained in:
Max Sokolov 2016-06-14 20:47:39 +03:00
parent 7cc572c7aa
commit cc75e84f58
3 changed files with 14 additions and 8 deletions

View File

@ -20,25 +20,25 @@
import UIKit import UIKit
public protocol HeightStrategy { public protocol HeightCalculatingStrategy {
var tableView: UITableView? { get set } var tableView: UITableView? { get set }
func height<Item, Cell: ConfigurableCell where Cell.T == Item, Cell: UITableViewCell>(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? public weak var tableView: UITableView?
private var cachedHeights = [Int: CGFloat]() private var cachedHeights = [Int: CGFloat]()
public func height<Item, Cell: ConfigurableCell where Cell.T == Item, Cell: UITableViewCell>(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.bounds = CGRectMake(0, 0, tableView?.bounds.size.width ?? 0, cell.bounds.height)
cell.configure(item) configure(cell: cell)
cell.setNeedsLayout() cell.setNeedsLayout()
cell.layoutIfNeeded() cell.layoutIfNeeded()

View File

@ -28,6 +28,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
public private(set) weak var tableView: UITableView? public private(set) weak var tableView: UITableView?
public private(set) var sections = [TableSection]() public private(set) var sections = [TableSection]()
private weak var scrollDelegate: UIScrollViewDelegate? private weak var scrollDelegate: UIScrollViewDelegate?
private var heightStrategy: HeightCalculatingStrategy?
public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil) { public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil) {
super.init() super.init()
@ -81,7 +82,12 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
} }
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 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 // MARK: UITableViewDataSource - configuration

View File

@ -60,7 +60,7 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
self.item = item self.item = item
actions?.forEach { self.actions[$0.type.key] = $0 } actions?.forEach { self.actions[$0.type.key] = $0 }
} }
// MARK: - RowConfigurable - // MARK: - RowConfigurable -
public func configure(cell: UITableViewCell) { public func configure(cell: UITableViewCell) {