try height strategy
This commit is contained in:
parent
7cc572c7aa
commit
cc75e84f58
|
|
@ -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()
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue