diff --git a/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift b/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift index 9d9d3ad..a84e42d 100644 --- a/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift +++ b/Demo/Classes/Presentation/Controllers/AutolayoutCellsController.swift @@ -13,12 +13,31 @@ class AutolayoutCellsController: UIViewController { @IBOutlet weak var tableView: UITableView! { didSet { - tableDirector = TableDirector(tableView: tableView) - tableDirector.shouldUsePrototypeCellHeightCalculation = true + tableDirector = TableDirector(tableView: tableView, shouldUsePrototypeCellHeightCalculation: true) } } var tableDirector: TableDirector! + func randomString(length: Int) -> String { + + let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + let len = UInt32(letters.length) + + var randomString = "" + + for _ in 0 ..< length { + let rand = arc4random_uniform(len) + var nextChar = letters.character(at: Int(rand)) + randomString += NSString(characters: &nextChar, length: 1) as String + } + + return randomString + } + + func randomInt(min: Int, max:Int) -> Int { + return min + Int(arc4random_uniform(UInt32(max - min + 1))) + } + override func viewDidLoad() { super.viewDidLoad() @@ -27,10 +46,10 @@ class AutolayoutCellsController: UIViewController { let section = TableSection() var rows = 0 - while rows <= 1000 { + while rows <= 20 { rows += 1 - let row = TableRow(item: ()) + let row = TableRow(item: randomString(length: randomInt(min: 20, max: 100))) section += row } diff --git a/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift b/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift index ff4219e..774fb66 100644 --- a/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift +++ b/Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift @@ -14,7 +14,7 @@ private let LoremIpsumBody = "Lorem ipsum dolor sit amet, consectetur adipisicin class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { - typealias T = Void + typealias T = String @IBOutlet var titleLabel: UILabel! @IBOutlet var subtitleLabel: UILabel! @@ -26,7 +26,7 @@ class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { func configure(with string: T) { titleLabel.text = LoremIpsumTitle - subtitleLabel.text = LoremIpsumBody + subtitleLabel.text = string } override func layoutSubviews() { @@ -37,4 +37,4 @@ class AutolayoutTableViewCell: UITableViewCell, ConfigurableCell { titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.size.width subtitleLabel.preferredMaxLayoutWidth = subtitleLabel.bounds.size.width } -} \ No newline at end of file +} diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index a829e68..88d4c99 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -32,6 +32,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { private var cellRegisterer: TableCellRegisterer? public var rowHeightCalculator: RowHeightCalculator? + @available(*, deprecated, message: "Produced incorrect behaviour") open var shouldUsePrototypeCellHeightCalculation: Bool = false { didSet { if shouldUsePrototypeCellHeightCalculation { @@ -46,13 +47,13 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { return sections.isEmpty } - public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil, shouldUseAutomaticCellRegistration: Bool = true) { + public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil, shouldUseAutomaticCellRegistration: Bool = true, cellHeightCalculator: RowHeightCalculator?) { super.init() if shouldUseAutomaticCellRegistration { self.cellRegisterer = TableCellRegisterer(tableView: tableView) } - + self.scrollDelegate = scrollDelegate self.tableView = tableView self.tableView?.delegate = self @@ -61,6 +62,13 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { NotificationCenter.default.addObserver(self, selector: #selector(didReceiveAction), name: NSNotification.Name(rawValue: TableKitNotifications.CellAction), object: nil) } + public convenience init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil, shouldUseAutomaticCellRegistration: Bool = true, shouldUsePrototypeCellHeightCalculation: Bool = false) { + + let heightCalculator = shouldUsePrototypeCellHeightCalculation ? TablePrototypeCellHeightCalculator(tableView: tableView) : nil + + self.init(tableView: tableView, scrollDelegate: scrollDelegate, shouldUseAutomaticCellRegistration: shouldUseAutomaticCellRegistration, cellHeightCalculator: heightCalculator) + } + deinit { NotificationCenter.default.removeObserver(self) }