move estimated height from builder to cell

This commit is contained in:
Max Sokolov 2016-02-21 13:20:58 +03:00
parent 619a5962d7
commit 4a9ed7f4ff
6 changed files with 26 additions and 17 deletions

View File

@ -158,7 +158,7 @@ public extension TableDirector {
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return builderAtIndexPath(indexPath).0.estimatedRowHeight
return CGFloat(builderAtIndexPath(indexPath).0.estimatedRowHeight)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

View File

@ -49,24 +49,24 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
private var items = [I]()
public var reusableIdentifier: String
public var estimatedRowHeight: CGFloat
public var estimatedRowHeight: Float {
return 44
}
public var numberOfRows: Int {
get {
return items.count
}
}
public init(item: I, id: String? = nil, estimatedRowHeight: CGFloat) {
public init(item: I, id: String? = nil) {
reusableIdentifier = id ?? NSStringFromClass(C).componentsSeparatedByString(".").last ?? ""
self.estimatedRowHeight = estimatedRowHeight
items.append(item)
}
public init(items: [I]? = nil, id: String? = nil, estimatedRowHeight: CGFloat) {
public init(items: [I]? = nil, id: String? = nil) {
reusableIdentifier = id ?? NSStringFromClass(C).componentsSeparatedByString(".").last ?? ""
self.estimatedRowHeight = estimatedRowHeight
if items != nil {
self.items.appendContentsOf(items!)
@ -128,13 +128,17 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
Responsible for building configurable cells of given type and passing items to them.
*/
public class TableConfigurableRowBuilder<I, C: ConfigurableCell where C.Item == I, C: UITableViewCell> : TableRowBuilder<I, C> {
public init(item: I, estimatedRowHeight: CGFloat) {
super.init(item: item, id: C.reusableIdentifier(), estimatedRowHeight: estimatedRowHeight)
public override var estimatedRowHeight: Float {
return C.estimatedHeight()
}
public init(items: [I]? = nil, estimatedRowHeight: CGFloat) {
super.init(items: items, id: C.reusableIdentifier(), estimatedRowHeight: estimatedRowHeight)
public init(item: I) {
super.init(item: item, id: C.reusableIdentifier())
}
public init(items: [I]? = nil) {
super.init(items: items, id: C.reusableIdentifier())
}
public override func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]? = nil) -> AnyObject? {

View File

@ -102,6 +102,7 @@ public protocol ConfigurableCell {
typealias Item
static func reusableIdentifier() -> String
static func estimatedHeight() -> Float
func configureWithItem(item: Item)
}
@ -121,7 +122,7 @@ public protocol RowBuilder {
var numberOfRows: Int { get }
var reusableIdentifier: String { get }
var estimatedRowHeight: CGFloat { get }
var estimatedRowHeight: Float { get }
func registerCell(inTableView tableView: UITableView)
func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject?

View File

@ -21,12 +21,16 @@ class ConfigurableTableViewCell: UITableViewCell, ConfigurableCell {
return "ConfigurableTableViewCell"
}
static func estimatedHeight() -> Float {
return 300
}
func configureWithItem(item: Item) {
button.setTitle("Button \(item)", forState: .Normal)
}
@IBAction func buttonClicked(sender: UIButton) {
Action(key: kConfigurableTableViewCellButtonClickedAction, sender: self).invoke()

View File

@ -19,7 +19,7 @@ class ViewController: UIViewController, UIScrollViewDelegate {
tableDirector = TableDirector(tableView: tableView)
tableDirector.scrollDelegate = self
let rowBuilder = TableRowBuilder<Int, UITableViewCell>(items: [1, 2, 3, 4], id: "cell", estimatedRowHeight: 44)
let rowBuilder = TableRowBuilder<Int, UITableViewCell>(items: [1, 2, 3, 4], id: "cell")
.action(.configure) { data in
data.cell?.textLabel?.text = "\(data.item)"
@ -34,7 +34,7 @@ class ViewController: UIViewController, UIScrollViewDelegate {
}
let configurableRowBuilder = TableConfigurableRowBuilder<String, ConfigurableTableViewCell>(items: ["5", "6", "7", "8"], estimatedRowHeight: 300)
let configurableRowBuilder = TableConfigurableRowBuilder<String, ConfigurableTableViewCell>(items: ["5", "6", "7", "8"])
.action(.click) { data -> Void in
print("click action indexPath: \(data.indexPath), item: \(data.item)")
@ -55,13 +55,13 @@ class ViewController: UIViewController, UIScrollViewDelegate {
data.cell!.contentLabel.text = "Tablet is a super lightweight yet powerful generic library that handles a complexity of UITableView's datasource and delegate methods in a Swift environment. Tablet's goal is to provide an easiest way to create complex table views. With Tablet you don't have to write a messy code of switch or if statements when you deal with bunch of different cells in different sections."
}
let myRowBuilder = TableRowBuilder<Int, MyTableViewCell>(item: 0, id: "cellll", estimatedRowHeight: 44)
let myRowBuilder = TableRowBuilder<Int, MyTableViewCell>(item: 0, id: "cellll")
let sectionBuilder = TableSectionBuilder(headerTitle: "Tablet", footerTitle: "Deal with table view like a boss.", rowBuilders: [rowBuilder, configurableRowBuilder, myRowBuilder])
tableDirector += sectionBuilder
sectionBuilder.appendRowBuilder(TableRowBuilder<Int, MyNibTableViewCell>(item: 0, estimatedRowHeight: 44))
sectionBuilder.appendRowBuilder(TableRowBuilder<Int, MyNibTableViewCell>(item: 0))
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {