diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 0e54392..59dccca 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -26,14 +26,14 @@ import Foundation */ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { - public private(set) weak var tableView: UITableView! + public unowned let tableView: UITableView public weak var scrollDelegate: UIScrollViewDelegate? private var sections = [TableSectionBuilder]() public init(tableView: UITableView) { - super.init() - + self.tableView = tableView + super.init() self.tableView.delegate = self self.tableView.dataSource = self @@ -41,7 +41,6 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate } deinit { - NSNotificationCenter.defaultCenter().removeObserver(self) } diff --git a/Tablet/TableSectionBuilder.swift b/Tablet/TableSectionBuilder.swift index 4bb0d68..90cddd7 100644 --- a/Tablet/TableSectionBuilder.swift +++ b/Tablet/TableSectionBuilder.swift @@ -29,10 +29,10 @@ public class TableSectionBuilder { weak var tableView: UITableView? private var builders = [RowBuilder]() - + public var headerTitle: String? public var footerTitle: String? - + public var headerView: UIView? public var footerView: UIView? @@ -41,18 +41,23 @@ public class TableSectionBuilder { return builders.reduce(0) { $0 + $1.numberOfRows } } - public init(headerTitle: String? = nil, footerTitle: String? = nil, rows: [RowBuilder]? = nil) { - - self.headerTitle = headerTitle - self.footerTitle = footerTitle - + public init(rows: [RowBuilder]? = nil) { + if let initialRows = rows { builders.appendContentsOf(initialRows) } } - public init(headerView: UIView? = nil, footerView: UIView? = nil) { + public convenience init(headerTitle: String?, footerTitle: String?, rows: [RowBuilder]?) { + self.init(rows: rows) + + self.headerTitle = headerTitle + self.footerTitle = footerTitle + } + public convenience init(headerView: UIView?, footerView: UIView?, rows: [RowBuilder]?) { + self.init(rows: rows) + self.headerView = headerView self.footerView = footerView } diff --git a/TabletDemo/Classes/Presentation/Controllers/HeaderFooterController.swift b/TabletDemo/Classes/Presentation/Controllers/HeaderFooterController.swift new file mode 100644 index 0000000..9666cca --- /dev/null +++ b/TabletDemo/Classes/Presentation/Controllers/HeaderFooterController.swift @@ -0,0 +1,33 @@ +// +// HeaderFooterController.swift +// TabletDemo +// +// Created by Max Sokolov on 16/04/16. +// Copyright © 2016 Tablet. All rights reserved. +// + +import UIKit +import Tablet + +class HeaderFooterController: UIViewController { + + @IBOutlet weak var tableView: UITableView! { + didSet { + tableDirector = TableDirector(tableView: tableView) + } + } + var tableDirector: TableDirector! + + override func viewDidLoad() { + super.viewDidLoad() + + let rows = TableConfigurableRowBuilder(items: ["3", "4", "5"]) + + let headerView = UIView(frame: CGRectMake(0, 0, 100, 100)) + headerView.backgroundColor = UIColor.lightGrayColor() + + let section = TableSectionBuilder(headerView: headerView, footerView: nil, rows: [rows]) + + tableDirector += section + } +} \ No newline at end of file diff --git a/TabletDemo/Classes/Presentation/Controllers/MainController.swift b/TabletDemo/Classes/Presentation/Controllers/MainController.swift new file mode 100644 index 0000000..f5213fe --- /dev/null +++ b/TabletDemo/Classes/Presentation/Controllers/MainController.swift @@ -0,0 +1,31 @@ +// +// MainController.swift +// TabletDemo +// +// Created by Max Sokolov on 16/04/16. +// Copyright © 2016 Tablet. All rights reserved. +// + +import UIKit +import Tablet + +class MainController: UIViewController { + + @IBOutlet weak var tableView: UITableView! { + didSet { + tableDirector = TableDirector(tableView: tableView) + } + } + var tableDirector: TableDirector! + + override func viewDidLoad() { + super.viewDidLoad() + + let rows = TableConfigurableRowBuilder(items: ["1", "2", "3"]) + .action(.click) { [unowned self] data -> Void in + self.performSegueWithIdentifier("headerfooter", sender: nil) + } + + tableDirector += rows + } +} \ No newline at end of file diff --git a/TabletDemo/Classes/Presentation/Main/ViewControllers/MainViewController.swift b/TabletDemo/Classes/Presentation/Main/ViewControllers/MainViewController.swift deleted file mode 100644 index 3bb415a..0000000 --- a/TabletDemo/Classes/Presentation/Main/ViewControllers/MainViewController.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// MainViewController.swift -// TabletDemo -// -// Created by Max Sokolov on 19/03/16. -// Copyright © 2016 Tablet. All rights reserved. -// - -import Foundation -import UIKit -import Tablet - -class MainViewController : UITableViewController { - - var tableDirector: TableDirector! - - override func viewDidLoad() { - super.viewDidLoad() - - tableDirector = TableDirector(tableView: tableView) - - tableDirector += TableRowBuilder(items: [1, 2, 3, 4], id: "cell") - .action(.configure) { data -> Void in - - data.cell?.accessoryType = .DisclosureIndicator - data.cell?.textLabel?.text = "\(data.item)" - } - .action(.click) { data -> Void in - - - } - } -} \ No newline at end of file diff --git a/TabletDemo/Classes/Presentation/Views/StoryboardTableViewCell.swift b/TabletDemo/Classes/Presentation/Views/StoryboardTableViewCell.swift new file mode 100644 index 0000000..2e4884b --- /dev/null +++ b/TabletDemo/Classes/Presentation/Views/StoryboardTableViewCell.swift @@ -0,0 +1,23 @@ +// +// StoryboardTableViewCell.swift +// TabletDemo +// +// Created by Max Sokolov on 16/04/16. +// Copyright © 2016 Tablet. All rights reserved. +// + +import UIKit +import Tablet + +class StoryboardTableViewCell: UITableViewCell, ConfigurableCell { + + typealias T = String + + func configure(value: T) { + textLabel?.text = value + } + + static func estimatedHeight() -> Float { + return 44 + } +} \ No newline at end of file diff --git a/TabletDemo/Resources/Storyboards/Main.storyboard b/TabletDemo/Resources/Storyboards/Main.storyboard index 2cf7895..1a1d1c2 100644 --- a/TabletDemo/Resources/Storyboards/Main.storyboard +++ b/TabletDemo/Resources/Storyboards/Main.storyboard @@ -1,39 +1,10 @@ - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -45,12 +16,101 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TabletDemo/TabletDemo.xcodeproj/project.pbxproj b/TabletDemo/TabletDemo.xcodeproj/project.pbxproj index 4d330c4..9779299 100644 --- a/TabletDemo/TabletDemo.xcodeproj/project.pbxproj +++ b/TabletDemo/TabletDemo.xcodeproj/project.pbxproj @@ -10,8 +10,10 @@ DAC2D5CA1C9D303E009E9C19 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC2D5C91C9D303E009E9C19 /* AppDelegate.swift */; }; DAC2D5CF1C9D30A7009E9C19 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAC2D5CD1C9D30A7009E9C19 /* Main.storyboard */; }; DAC2D5D01C9D30A7009E9C19 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAC2D5CE1C9D30A7009E9C19 /* LaunchScreen.storyboard */; }; - DAC2D5D41C9D3118009E9C19 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC2D5D31C9D3118009E9C19 /* MainViewController.swift */; }; DAC2D69C1C9E75E3009E9C19 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DAC2D69B1C9E75E3009E9C19 /* Assets.xcassets */; }; + DACB71761CC2D63D00432BD3 /* MainController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DACB71751CC2D63D00432BD3 /* MainController.swift */; }; + DACB71781CC2D6ED00432BD3 /* StoryboardTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DACB71771CC2D6ED00432BD3 /* StoryboardTableViewCell.swift */; }; + DACB717A1CC2D89D00432BD3 /* HeaderFooterController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DACB71791CC2D89D00432BD3 /* HeaderFooterController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -19,9 +21,11 @@ DAC2D5C91C9D303E009E9C19 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; DAC2D5CD1C9D30A7009E9C19 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = ""; }; DAC2D5CE1C9D30A7009E9C19 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = ""; }; - DAC2D5D31C9D3118009E9C19 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; DAC2D69B1C9E75E3009E9C19 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; DAC2D69D1C9E78B5009E9C19 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + DACB71751CC2D63D00432BD3 /* MainController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainController.swift; sourceTree = ""; }; + DACB71771CC2D6ED00432BD3 /* StoryboardTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardTableViewCell.swift; sourceTree = ""; }; + DACB71791CC2D89D00432BD3 /* HeaderFooterController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeaderFooterController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -64,7 +68,8 @@ DAC2D5C71C9D3005009E9C19 /* Presentation */ = { isa = PBXGroup; children = ( - DAC2D5D11C9D30D8009E9C19 /* Main */, + DACB71731CC2D5ED00432BD3 /* Controllers */, + DACB71741CC2D5FD00432BD3 /* Views */, ); path = Presentation; sourceTree = ""; @@ -96,22 +101,6 @@ path = Storyboards; sourceTree = ""; }; - DAC2D5D11C9D30D8009E9C19 /* Main */ = { - isa = PBXGroup; - children = ( - DAC2D5D21C9D30E4009E9C19 /* ViewControllers */, - ); - path = Main; - sourceTree = ""; - }; - DAC2D5D21C9D30E4009E9C19 /* ViewControllers */ = { - isa = PBXGroup; - children = ( - DAC2D5D31C9D3118009E9C19 /* MainViewController.swift */, - ); - path = ViewControllers; - sourceTree = ""; - }; DAC2D69A1C9E75BE009E9C19 /* Assets */ = { isa = PBXGroup; children = ( @@ -120,6 +109,23 @@ path = Assets; sourceTree = ""; }; + DACB71731CC2D5ED00432BD3 /* Controllers */ = { + isa = PBXGroup; + children = ( + DACB71751CC2D63D00432BD3 /* MainController.swift */, + DACB71791CC2D89D00432BD3 /* HeaderFooterController.swift */, + ); + path = Controllers; + sourceTree = ""; + }; + DACB71741CC2D5FD00432BD3 /* Views */ = { + isa = PBXGroup; + children = ( + DACB71771CC2D6ED00432BD3 /* StoryboardTableViewCell.swift */, + ); + path = Views; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -192,8 +198,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - DAC2D5D41C9D3118009E9C19 /* MainViewController.swift in Sources */, + DACB71781CC2D6ED00432BD3 /* StoryboardTableViewCell.swift in Sources */, + DACB71761CC2D63D00432BD3 /* MainController.swift in Sources */, DAC2D5CA1C9D303E009E9C19 /* AppDelegate.swift in Sources */, + DACB717A1CC2D89D00432BD3 /* HeaderFooterController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tests/TabletTests.swift b/Tests/TabletTests.swift index 0cbe395..b8d3e07 100644 --- a/Tests/TabletTests.swift +++ b/Tests/TabletTests.swift @@ -157,7 +157,7 @@ class TabletTests: XCTestCase { let sectionHeaderView = UIView() let sectionFooterView = UIView() - let section = TableSectionBuilder(headerView: sectionHeaderView, footerView: sectionFooterView) + let section = TableSectionBuilder(headerView: sectionHeaderView, footerView: sectionFooterView, rows: nil) section += row testController.view.hidden = false