add row builder

This commit is contained in:
Max Sokolov 2016-06-12 23:42:39 +03:00
parent 310cec9c12
commit 533c6e6eb8
5 changed files with 73 additions and 16 deletions

View File

@ -16,6 +16,7 @@
DA9EA7781D0B68460021F650 /* TableSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA76F1D0B68460021F650 /* TableSection.swift */; };
DA9EA7791D0B68460021F650 /* TableCellAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA7701D0B68460021F650 /* TableCellAction.swift */; };
DA9EA7801D0B689C0021F650 /* TableKit.h in Headers */ = {isa = PBXBuildFile; fileRef = DA9EA77E1D0B689C0021F650 /* TableKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
DA9EA7861D0DFAD60021F650 /* TableRowBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA7851D0DFAD60021F650 /* TableRowBuilder.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -30,6 +31,7 @@
DA9EA7701D0B68460021F650 /* TableCellAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableCellAction.swift; sourceTree = "<group>"; };
DA9EA77D1D0B689C0021F650 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DA9EA77E1D0B689C0021F650 /* TableKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableKit.h; sourceTree = "<group>"; };
DA9EA7851D0DFAD60021F650 /* TableRowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRowBuilder.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -66,6 +68,7 @@
DA9EA76B1D0B68460021F650 /* TableDirector.swift */,
DA9EA76F1D0B68460021F650 /* TableSection.swift */,
DA9EA76C1D0B68460021F650 /* TableRow.swift */,
DA9EA7851D0DFAD60021F650 /* TableRowBuilder.swift */,
DA9EA76D1D0B68460021F650 /* TableRowAction.swift */,
DA9EA7701D0B68460021F650 /* TableCellAction.swift */,
DA9EA7681D0B68460021F650 /* ConfigurableCell.swift */,
@ -168,6 +171,7 @@
DA9EA7751D0B68460021F650 /* TableRow.swift in Sources */,
DA9EA7761D0B68460021F650 /* TableRowAction.swift in Sources */,
DA9EA7741D0B68460021F650 /* TableDirector.swift in Sources */,
DA9EA7861D0DFAD60021F650 /* TableRowBuilder.swift in Sources */,
DA9EA7791D0B68460021F650 /* TableCellAction.swift in Sources */,
DA9EA7731D0B68460021F650 /* Operators.swift in Sources */,
);

View File

@ -79,7 +79,7 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
// MARK: - actions -
public func action(action: TableRowAction<ItemType, CellType>) -> Self {
public func addAction(action: TableRowAction<ItemType, CellType>) -> Self {
actions[action.type.key] = action
return self

View File

@ -0,0 +1,52 @@
//
// Copyright (c) 2015 Max Sokolov https://twitter.com/max_sokolov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
public protocol RowBuilder {
func rowItems() -> [Row]?
}
public class TableRowBuilder<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: RowBuilder {
public typealias BuilderHandler = (TableRowBuilder) -> ()
public var items: [ItemType]?
public var actions: [TableRowAction<ItemType, CellType>]?
public init(handler: BuilderHandler) {
handler(self)
}
// MARK: - RowBuilder -
public func rowItems() -> [Row]? {
return items?.map { TableRow<ItemType, CellType>(item: $0, actions: actions) }
}
}
public extension TableSection {
public func append(builder builder: RowBuilder) {
if let rows = builder.rowItems() {
append(rows: rows)
}
}
}

View File

@ -48,14 +48,14 @@ public class TableSection {
}
}
public convenience init(headerTitle: String?, footerTitle: String?, rows: [Row]?) {
public convenience init(headerTitle: String?, footerTitle: String?, rows: [Row]? = nil) {
self.init(rows: rows)
self.headerTitle = headerTitle
self.footerTitle = footerTitle
}
public convenience init(headerView: UIView?, footerView: UIView?, rows: [Row]?) {
public convenience init(headerView: UIView?, footerView: UIView?, rows: [Row]? = nil) {
self.init(rows: rows)
self.headerView = headerView

View File

@ -21,22 +21,20 @@ class MainController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/*let rowBuilder = TableRowBuilder<String, StoryboardImageTableViewCell>(items: ["1", "1", "1", "1"])
.action(.click) { [unowned self] data in
self.performSegueWithIdentifier("headerfooter", sender: nil)
}*/
let a = TableRowAction<String, StoryboardImageTableViewCell>(.click) {
(row) in
(data) in
print("3")
print("3", data.item)
}
let b = TableRowBuilder<String, StoryboardImageTableViewCell> {
$0.items = ["1", "2", "3"]
$0.actions = [a]
}
let row1 = TableRow<String, StoryboardImageTableViewCell>(item: "1")
let row2 = TableRow<String, StoryboardImageTableViewCell>(item: "2")
@ -45,13 +43,13 @@ class MainController: UIViewController {
row1
.action(TableRowAction(.shouldHighlight) { (data) -> Bool in
.addAction(TableRowAction(.shouldHighlight) { (data) -> Bool in
print("1")
return false
})
.action(TableRowAction(.click) { (data) in
.addAction(TableRowAction(.click) { (data) in
print("2")
@ -59,7 +57,10 @@ class MainController: UIViewController {
})
let section = TableSection(headerTitle: "", footerTitle: "", rows: [row1, row2, row3])
let section = TableSection()
section.append(builder: b)
//let section = TableSection(headerTitle: "", footerTitle: "", rows: [row1, row2, row3])
tableDirector += [section]