Skip to content

Commit 7f0e948

Browse files
committed
adding preset controller and tools to make custom ones
1 parent 764df1e commit 7f0e948

File tree

10 files changed

+216
-33
lines changed

10 files changed

+216
-33
lines changed

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Presentables.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
1541CA39200C349800CE03C3 /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156D7AA41F422E570075ACE7 /* TableViewCell.swift */; };
11+
156801342091176500FA296A /* ManagerTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156801332091176500FA296A /* ManagerTableViewController.swift */; };
1112
156D7AFA1F431A150075ACE7 /* TableViewHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156D7AF91F431A150075ACE7 /* TableViewHeader.swift */; };
1213
158B4B1D206410E300FCE847 /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 158B4B1C206410E300FCE847 /* CollectionViewController.swift */; };
1314
158B4B1F2064111500FCE847 /* CollectionDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 158B4B1E2064111500FCE847 /* CollectionDataManager.swift */; };
@@ -33,6 +34,7 @@
3334
/* End PBXContainerItemProxy section */
3435

3536
/* Begin PBXFileReference section */
37+
156801332091176500FA296A /* ManagerTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ManagerTableViewController.swift; sourceTree = "<group>"; };
3638
156D7AA41F422E570075ACE7 /* TableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewCell.swift; sourceTree = "<group>"; };
3739
156D7AF91F431A150075ACE7 /* TableViewHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewHeader.swift; sourceTree = "<group>"; };
3840
158B4B1C206410E300FCE847 /* CollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = "<group>"; };
@@ -85,6 +87,7 @@
8587
children = (
8688
158B4B1C206410E300FCE847 /* CollectionViewController.swift */,
8789
607FACD71AFB9204008FA782 /* TableViewController.swift */,
90+
156801332091176500FA296A /* ManagerTableViewController.swift */,
8891
15CA94761FD0408F001AA1FE /* ViewController.swift */,
8992
);
9093
name = "View controllers";
@@ -418,6 +421,7 @@
418421
buildActionMask = 2147483647;
419422
files = (
420423
159C16D5200C3DA0008BE539 /* TableDataManager.swift in Sources */,
424+
156801342091176500FA296A /* ManagerTableViewController.swift in Sources */,
421425
607FACD81AFB9204008FA782 /* TableViewController.swift in Sources */,
422426
158B4B2620641A4800FCE847 /* CollectionViewCell.swift in Sources */,
423427
15CA94771FD0408F001AA1FE /* ViewController.swift in Sources */,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// ManagerTableViewController.swift
3+
// Presentables_Example
4+
//
5+
// Created by Ondrej Rafaj on 25/04/2018.
6+
// Copyright © 2018 CocoaPods. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Presentables
11+
12+
13+
class ManagerTableViewController: PresentableTableViewController {
14+
15+
override func viewDidLoad() {
16+
super.viewDidLoad()
17+
18+
let section = PresentableSection()
19+
20+
let header = Presentable<TableViewHeader>.create { (header) in
21+
header.titleLabel.text = "It works yet again!"
22+
}
23+
section.header = header
24+
25+
let presentable = Presentable<TableViewCell2>.create({ (cell) in
26+
cell.textLabel?.text = "Custom cell"
27+
}).cellSelected {
28+
print("First cell has been selected")
29+
}
30+
section.presentables.append(presentable)
31+
32+
data.append(section)
33+
34+
presentableManager.selectedCell = { info in
35+
info.tableView.deselectRow(at: info.indexPath, animated: true)
36+
print("Did select cell no. \((info.indexPath.row + 1))")
37+
}
38+
}
39+
40+
}

Example/Presentables/TableDataManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TableDataManager: PresentableTableViewDataManager {
4848

4949
// MARK: Overriding table view delegate (optional)
5050

51-
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
51+
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
5252
return 44
5353
}
5454

Example/Presentables/ViewController.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ class ViewController: UIViewController {
3737
make.height.equalTo(44)
3838
}
3939

40+
let tableButton2 = UIButton()
41+
tableButton2.backgroundColor = tableButton2.tintColor
42+
tableButton2.layer.cornerRadius = 6
43+
tableButton2.setTitle("Table view example 2", for: .normal)
44+
tableButton2.setTitleColor(.white, for: .normal)
45+
view.addSubview(tableButton2)
46+
tableButton2.addTarget(self, action: #selector(didTapTable2Button), for: .touchUpInside)
47+
tableButton2.snp.makeConstraints { (make) in
48+
make.top.equalTo(tableButton.snp.bottom).offset(20)
49+
make.left.right.height.equalTo(tableButton)
50+
}
51+
4052
let collectionButton = UIButton()
4153
collectionButton.backgroundColor = collectionButton.tintColor
4254
collectionButton.layer.cornerRadius = 6
@@ -45,8 +57,8 @@ class ViewController: UIViewController {
4557
view.addSubview(collectionButton)
4658
collectionButton.addTarget(self, action: #selector(didTapCollectionButton), for: .touchUpInside)
4759
collectionButton.snp.makeConstraints { (make) in
48-
make.top.equalTo(tableButton.snp.bottom).offset(20)
49-
make.left.right.height.equalTo(tableButton)
60+
make.top.equalTo(tableButton2.snp.bottom).offset(20)
61+
make.left.right.height.equalTo(tableButton2)
5062
}
5163
}
5264

@@ -57,6 +69,11 @@ class ViewController: UIViewController {
5769
navigationController?.pushViewController(c, animated: true)
5870
}
5971

72+
@objc func didTapTable2Button() {
73+
let c = ManagerTableViewController()
74+
navigationController?.pushViewController(c, animated: true)
75+
}
76+
6077
@objc func didTapCollectionButton() {
6178
let c = CollectionViewController()
6279
navigationController?.pushViewController(c, animated: true)

Presentables.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'Presentables'
11-
s.version = '0.6.2'
11+
s.version = '0.6.3'
1212
s.summary = 'Simple reactive library for managing table views & collection views, written in Swift'
1313

1414
# This description is used to generate tags and improve search results.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// PresentableTableViewController.swift
3+
// Presentables
4+
//
5+
// Created by Ondrej Rafaj on 25/04/2018.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
12+
open class PresentableTableViewController: UITableViewController {
13+
14+
public let presentableManager = PresentableTableViewDataManager()
15+
16+
public var data: PresentableSections {
17+
get { return presentableManager.data }
18+
set { presentableManager.data = newValue }
19+
}
20+
21+
// MARK: View lifecycle
22+
23+
open override func viewDidLoad() {
24+
super.viewDidLoad()
25+
26+
var manager: PresentableManager = presentableManager
27+
tableView.bind(withPresentableManager: &manager)
28+
}
29+
30+
}

Presentables/Classes/Table views/PresentableTableViewDataManager.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ open class PresentableTableViewDataManager: NSObject, PresentableManager, UITabl
3232

3333
// MARK: Actions
3434

35-
public func reloadData() {
35+
open func reloadData() {
3636
tableView?.reloadData()
3737
}
3838

39-
public func reload(section: Int) {
39+
open func reload(section: Int) {
4040
tableView?.reloadSections([section], with: .none)
4141
}
4242

43-
public func reload(indexPath: IndexPath) {
43+
open func reload(indexPath: IndexPath) {
4444
tableView?.reloadRows(at: [indexPath], with: .none)
4545
}
4646

@@ -68,46 +68,46 @@ open class PresentableTableViewDataManager: NSObject, PresentableManager, UITabl
6868
return cell!
6969
}
7070

71+
// MARK: Private helpers
72+
73+
func tableView(_ tableView: UITableView, presentable: AnyPresentable) -> UIView? {
74+
let identifier: String = presentable.identifier
75+
var view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
76+
if view == nil {
77+
tableView.register(presentable.storedType, forHeaderFooterViewReuseIdentifier: presentable.identifier)
78+
view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
79+
guard view != nil else {
80+
return nil
81+
}
82+
}
83+
presentable.runConfigure(with: view)
84+
return view
85+
}
86+
87+
// MARK: Delegate
88+
7189
open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
7290
guard let presentable: AnyPresentable = data.header(forSection: section) else {
7391
return nil
7492
}
7593
return self.tableView(tableView, presentable: presentable)
7694
}
7795

78-
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
96+
open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
7997
return data[section].header == nil ? 0 : UITableViewAutomaticDimension
8098
}
81-
99+
82100
open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
83101
guard let presentable: AnyPresentable = data.footer(forSection: section) else {
84102
return nil
85103
}
86104
return self.tableView(tableView, presentable: presentable)
87105
}
88106

89-
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
107+
open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
90108
return data[section].footer == nil ? 0 : UITableViewAutomaticDimension
91109
}
92110

93-
// MARK: Private helpers
94-
95-
func tableView(_ tableView: UITableView, presentable: AnyPresentable) -> UIView? {
96-
let identifier: String = presentable.identifier
97-
var view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
98-
if view == nil {
99-
tableView.register(presentable.storedType, forHeaderFooterViewReuseIdentifier: presentable.identifier)
100-
view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
101-
guard view != nil else {
102-
return nil
103-
}
104-
}
105-
presentable.runConfigure(with: view)
106-
return view
107-
}
108-
109-
// MARK: Delegate
110-
111111
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
112112
let presentable: AnyPresentable = data.presentable(forIndexPath: indexPath)
113113
presentable.selected?()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// PresentableTableViewManageable.swift
3+
// Presentables
4+
//
5+
// Created by Ondrej Rafaj on 25/04/2018.
6+
//
7+
8+
import Foundation
9+
10+
11+
public protocol PresentableTableViewManageable {
12+
var tableView: UITableView { get }
13+
var presentableManager: PresentableTableViewDataManager { get }
14+
var data: PresentableSections { get set }
15+
}
16+
17+
18+
extension PresentableTableViewManageable {
19+
20+
func bind() {
21+
var manager: PresentableManager = presentableManager
22+
tableView.bind(withPresentableManager: &manager)
23+
}
24+
25+
}

0 commit comments

Comments
 (0)