Skip to content

Commit 453c5bf

Browse files
committed
Fixed the source file in pod spec.
1 parent c5ad8d8 commit 453c5bf

5 files changed

Lines changed: 313 additions & 2 deletions
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//
2+
// CollapsibleTableSectionViewController.swift
3+
// CollapsibleTableSectionViewController
4+
//
5+
// Created by Yong Su on 7/20/17.
6+
// Copyright © 2017 jeantimex. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
//
12+
// MARK: - Section Data Structure
13+
//
14+
struct Item {
15+
var name: String
16+
var detail: String
17+
18+
init(name: String, detail: String) {
19+
self.name = name
20+
self.detail = detail
21+
}
22+
}
23+
24+
struct Section {
25+
var name: String
26+
var items: [Item]
27+
var collapsed: Bool
28+
29+
init(name: String, items: [Item], collapsed: Bool = false) {
30+
self.name = name
31+
self.items = items
32+
self.collapsed = collapsed
33+
}
34+
}
35+
36+
//
37+
// MARK: - View Controller
38+
//
39+
class CollapsibleTableSectionViewController: UITableViewController {
40+
41+
var sections = [Section]()
42+
43+
override func viewDidLoad() {
44+
super.viewDidLoad()
45+
46+
// Auto resizing the height of the cell
47+
tableView.estimatedRowHeight = 44.0
48+
tableView.rowHeight = UITableViewAutomaticDimension
49+
}
50+
51+
}
52+
53+
//
54+
// MARK: - View Controller DataSource and Delegate
55+
//
56+
extension CollapsibleTableSectionViewController {
57+
58+
override func numberOfSections(in tableView: UITableView) -> Int {
59+
return sections.count
60+
}
61+
62+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
63+
return sections[section].collapsed ? 0 : sections[section].items.count
64+
}
65+
66+
// Cell
67+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
68+
let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ??
69+
CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell")
70+
71+
let item: Item = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row]
72+
73+
cell.nameLabel.text = item.name
74+
cell.detailLabel.text = item.detail
75+
76+
return cell
77+
}
78+
79+
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
80+
return sections[(indexPath as NSIndexPath).section].collapsed ? 0 : UITableViewAutomaticDimension
81+
}
82+
83+
// Header
84+
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
85+
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
86+
87+
header.titleLabel.text = sections[section].name
88+
header.arrowLabel.text = ">"
89+
header.setCollapsed(sections[section].collapsed)
90+
91+
header.section = section
92+
header.delegate = self
93+
94+
return header
95+
}
96+
97+
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
98+
return 44.0
99+
}
100+
101+
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
102+
return 1.0
103+
}
104+
105+
}
106+
107+
//
108+
// MARK: - Section Header Delegate
109+
//
110+
extension CollapsibleTableSectionViewController: CollapsibleTableViewHeaderDelegate {
111+
112+
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
113+
let collapsed = !sections[section].collapsed
114+
115+
// Toggle collapse
116+
sections[section].collapsed = collapsed
117+
header.setCollapsed(collapsed)
118+
119+
tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
120+
}
121+
122+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// CollapsibleTableViewCell.swift
3+
// CollapsibleTableSectionViewController
4+
//
5+
// Created by Yong Su on 7/20/17.
6+
// Copyright © 2017 jeantimex. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class CollapsibleTableViewCell: UITableViewCell {
12+
13+
let nameLabel = UILabel()
14+
let detailLabel = UILabel()
15+
16+
// MARK: Initalizers
17+
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
18+
super.init(style: style, reuseIdentifier: reuseIdentifier)
19+
20+
let marginGuide = contentView.layoutMarginsGuide
21+
22+
// configure nameLabel
23+
contentView.addSubview(nameLabel)
24+
nameLabel.translatesAutoresizingMaskIntoConstraints = false
25+
nameLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
26+
nameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
27+
nameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
28+
nameLabel.numberOfLines = 0
29+
nameLabel.font = UIFont.systemFont(ofSize: 16)
30+
31+
// configure detailLabel
32+
contentView.addSubview(detailLabel)
33+
detailLabel.lineBreakMode = .byWordWrapping
34+
detailLabel.translatesAutoresizingMaskIntoConstraints = false
35+
detailLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
36+
detailLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
37+
detailLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
38+
detailLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 5).isActive = true
39+
detailLabel.numberOfLines = 0
40+
detailLabel.font = UIFont.systemFont(ofSize: 12)
41+
detailLabel.textColor = UIColor.lightGray
42+
}
43+
44+
required init?(coder aDecoder: NSCoder) {
45+
fatalError("init(coder:) has not been implemented")
46+
}
47+
48+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// CollapsibleTableViewHeader.swift
3+
// CollapsibleTableSectionViewController
4+
//
5+
// Created by Yong Su on 7/20/17.
6+
// Copyright © 2017 jeantimex. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
protocol CollapsibleTableViewHeaderDelegate {
12+
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int)
13+
}
14+
15+
class CollapsibleTableViewHeader: UITableViewHeaderFooterView {
16+
17+
var delegate: CollapsibleTableViewHeaderDelegate?
18+
var section: Int = 0
19+
20+
let titleLabel = UILabel()
21+
let arrowLabel = UILabel()
22+
23+
override init(reuseIdentifier: String?) {
24+
super.init(reuseIdentifier: reuseIdentifier)
25+
26+
//
27+
// Constraint the size of arrow label for auto layout
28+
//
29+
arrowLabel.widthAnchor.constraint(equalToConstant: 12).isActive = true
30+
31+
titleLabel.translatesAutoresizingMaskIntoConstraints = false
32+
arrowLabel.translatesAutoresizingMaskIntoConstraints = false
33+
34+
contentView.addSubview(titleLabel)
35+
contentView.addSubview(arrowLabel)
36+
37+
//
38+
// Call tapHeader when tapping on this header
39+
//
40+
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:))))
41+
}
42+
43+
required init?(coder aDecoder: NSCoder) {
44+
fatalError("init(coder:) has not been implemented")
45+
}
46+
47+
override func layoutSubviews() {
48+
super.layoutSubviews()
49+
50+
contentView.backgroundColor = UIColor(hex: 0x2E3944)
51+
52+
titleLabel.textColor = UIColor.white
53+
arrowLabel.textColor = UIColor.white
54+
55+
//
56+
// Autolayout the lables
57+
//
58+
let views = [
59+
"titleLabel" : titleLabel,
60+
"arrowLabel" : arrowLabel,
61+
]
62+
63+
contentView.addConstraints(NSLayoutConstraint.constraints(
64+
withVisualFormat: "H:|-20-[titleLabel]-[arrowLabel]-20-|",
65+
options: [],
66+
metrics: nil,
67+
views: views
68+
))
69+
70+
contentView.addConstraints(NSLayoutConstraint.constraints(
71+
withVisualFormat: "V:|-[titleLabel]-|",
72+
options: [],
73+
metrics: nil,
74+
views: views
75+
))
76+
77+
contentView.addConstraints(NSLayoutConstraint.constraints(
78+
withVisualFormat: "V:|-[arrowLabel]-|",
79+
options: [],
80+
metrics: nil,
81+
views: views
82+
))
83+
}
84+
85+
//
86+
// Trigger toggle section when tapping on the header
87+
//
88+
func tapHeader(_ gestureRecognizer: UITapGestureRecognizer) {
89+
guard let cell = gestureRecognizer.view as? CollapsibleTableViewHeader else {
90+
return
91+
}
92+
93+
delegate?.toggleSection(self, section: cell.section)
94+
}
95+
96+
func setCollapsed(_ collapsed: Bool) {
97+
//
98+
// Animate the arrow rotation (see Extensions.swf)
99+
//
100+
arrowLabel.rotate(collapsed ? 0.0 : .pi / 2)
101+
}
102+
103+
}
104+

Classes/Extensions.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Extensions.swift
3+
// CollapsibleTableSectionViewController
4+
//
5+
// Created by Yong Su on 7/20/17.
6+
// Copyright © 2017 jeantimex. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIColor {
12+
13+
convenience init(hex:Int, alpha:CGFloat = 1.0) {
14+
self.init(
15+
red: CGFloat((hex & 0xFF0000) >> 16) / 255.0,
16+
green: CGFloat((hex & 0x00FF00) >> 8) / 255.0,
17+
blue: CGFloat((hex & 0x0000FF) >> 0) / 255.0,
18+
alpha: alpha
19+
)
20+
}
21+
22+
}
23+
24+
extension UIView {
25+
26+
func rotate(_ toValue: CGFloat, duration: CFTimeInterval = 0.2) {
27+
let animation = CABasicAnimation(keyPath: "transform.rotation")
28+
29+
animation.toValue = toValue
30+
animation.duration = duration
31+
animation.isRemovedOnCompletion = false
32+
animation.fillMode = kCAFillModeForwards
33+
34+
self.layer.add(animation, forKey: nil)
35+
}
36+
37+
}

CollapsibleTableSectionViewController.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "CollapsibleTableSectionViewController"
19-
s.version = "0.0.1"
19+
s.version = "0.0.2"
2020
s.summary = "Swift library to support collapsible sections in a table view."
2121

2222
# This description is used to generate tags and improve search results.
@@ -91,7 +91,7 @@ Pod::Spec.new do |s|
9191
# Not including the public_header_files will make all headers public.
9292
#
9393

94-
s.source_files = "Classes", "Classes/**/*.{h,m}"
94+
s.source_files = "Classes/**/*.swift"
9595
s.exclude_files = "Classes/Exclude"
9696

9797
# s.public_header_files = "Classes/**/*.h"

0 commit comments

Comments
 (0)