add RowConfigurable protocol

This commit is contained in:
Max Sokolov 2016-06-07 21:41:57 +03:00
parent 0c5f24a491
commit 21f131bb19
5 changed files with 64 additions and 12 deletions

View File

@ -26,7 +26,12 @@ public protocol RowHeightCalculatable {
func estimatedRowHeight(index: Int, indexPath: NSIndexPath) -> CGFloat
}
public protocol RowBuilder: RowHeightCalculatable {
public protocol RowConfigurable {
func configure(cell: UITableViewCell, path: NSIndexPath, index: Int)
}
public protocol RowBuilder: RowConfigurable, RowHeightCalculatable {
var tableDirector: TableDirector? { get }

View File

@ -119,8 +119,8 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
cell.layoutIfNeeded()
}
builder.0.invoke(action: .configure, cell: cell, indexPath: indexPath, itemIndex: builder.1, userInfo: nil)
builder.0.configure(cell, path: indexPath, index: builder.1)
return cell
}

View File

@ -22,12 +22,18 @@ import UIKit
public protocol RowItemable {
var reusableIdentifier: String { get }
func configure(cell: UITableViewCell)
}
public class RowItem<DataType, CellType: ConfigurableCell where CellType.T == DataType>: RowItemable {
public let item: DataType
public var reusableIdentifier: String {
return CellType.reusableIdentifier()
}
public init(item: DataType) {
self.item = item
@ -38,7 +44,7 @@ public class RowItem<DataType, CellType: ConfigurableCell where CellType.T == Da
}
}
public class TableDynamicRowBuilder {
public class TableDynamicRowBuilder: RowBuilder {
var items = [RowItemable]()
@ -53,4 +59,41 @@ public class TableDynamicRowBuilder {
cellItem.configure(cell)
}
// MARK: - RowConfigurable -
public func configure(cell: UITableViewCell, path: NSIndexPath, index: Int) {
}
// MARK: - RowBuilder -
public private(set) weak var tableDirector: TableDirector?
public var numberOfRows: Int {
return items.count
}
public func reusableIdentifier(index: Int) -> String {
return items[index].reusableIdentifier
}
public func willUpdateDirector(director: TableDirector?) {
}
public func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject : AnyObject]?) -> AnyObject? {
return nil
}
// MARK: - RowHeightCalculatable -
public func rowHeight(index: Int, indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
public func estimatedRowHeight(index: Int, indexPath: NSIndexPath) -> CGFloat {
return 0 //CellType.estimatedHeight()
}
}

View File

@ -64,6 +64,13 @@ public class TableRowBuilder<DataType, CellType: ConfigurableCell where CellType
return CellType.estimatedHeight()
}
// MARK: - RowConfigurable -
public func configure(cell: UITableViewCell, path: NSIndexPath, index: Int) {
(cell as? CellType)?.configure(items[index])
}
// MARK: - Chaining actions -
public func addAction(action: TableRowAction<DataType, CellType>) {

View File

@ -26,17 +26,15 @@ class MainController: UIViewController {
self.performSegueWithIdentifier("headerfooter", sender: nil)
}
let section = TableSectionBuilder(headerTitle: "", footerTitle: "", rows: [rowBuilder])
/*let rows2 = TableRowBuilder<String, StoryboardImageTableViewCell>(items: ["1", "1", "1", "1"])
let cellItem = RowItem<String, StoryboardImageTableViewCell>(item: "1")
let cellItem = RowItem<String, StoryboardImageTableViewCell>(item: "1")
let cellItem2 = RowItem<String, StoryboardImageTableViewCell>(item: "1")
let cellItem3 = RowItem<String, StoryboardImageTableViewCell>(item: "1")
@ -45,7 +43,7 @@ class MainController: UIViewController {
let b = TableDynamicRowBuilder(items: [cellItem, cellItem2, cellItem3])
rowBuilder
/*rowBuilder
.addAction(TableRowAction(type: .Click) { (data) in
@ -55,8 +53,7 @@ class MainController: UIViewController {
.delete(indexes: [0], animated: .None)
.insert(["2"], atIndex: 0, animated: .None)
.update(index: 0, item: "", animated: .None)
.move([1, 2], toIndexes: [3, 4])*/
.move([1, 2], toIndexes: [3, 4])
@ -71,7 +68,7 @@ class MainController: UIViewController {
//tableView.insertSections([], withRowAnimation: .None)
//tableDirector.performBatchUpdates {
//}
//}*/
tableDirector.append(section: section)
}