bugfixes caused by RowHeightCalculator

This commit is contained in:
Max Sokolov 2016-11-16 17:00:36 +03:00
parent d4fc924676
commit 065d8ccae3
3 changed files with 36 additions and 9 deletions

View File

@ -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<AutolayoutTableViewCell>(item: ())
let row = TableRow<AutolayoutTableViewCell>(item: randomString(length: randomInt(min: 20, max: 100)))
section += row
}

View File

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

View File

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