-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jeantimex/fix-source-files-in-pod-spec
Fixed the source file in pod spec.
- Loading branch information
Showing
5 changed files
with
313 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// CollapsibleTableSectionViewController.swift | ||
// CollapsibleTableSectionViewController | ||
// | ||
// Created by Yong Su on 7/20/17. | ||
// Copyright © 2017 jeantimex. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
// | ||
// MARK: - Section Data Structure | ||
// | ||
struct Item { | ||
var name: String | ||
var detail: String | ||
|
||
init(name: String, detail: String) { | ||
self.name = name | ||
self.detail = detail | ||
} | ||
} | ||
|
||
struct Section { | ||
var name: String | ||
var items: [Item] | ||
var collapsed: Bool | ||
|
||
init(name: String, items: [Item], collapsed: Bool = false) { | ||
self.name = name | ||
self.items = items | ||
self.collapsed = collapsed | ||
} | ||
} | ||
|
||
// | ||
// MARK: - View Controller | ||
// | ||
class CollapsibleTableSectionViewController: UITableViewController { | ||
|
||
var sections = [Section]() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
// Auto resizing the height of the cell | ||
tableView.estimatedRowHeight = 44.0 | ||
tableView.rowHeight = UITableViewAutomaticDimension | ||
} | ||
|
||
} | ||
|
||
// | ||
// MARK: - View Controller DataSource and Delegate | ||
// | ||
extension CollapsibleTableSectionViewController { | ||
|
||
override func numberOfSections(in tableView: UITableView) -> Int { | ||
return sections.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return sections[section].collapsed ? 0 : sections[section].items.count | ||
} | ||
|
||
// Cell | ||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ?? | ||
CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell") | ||
|
||
let item: Item = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row] | ||
|
||
cell.nameLabel.text = item.name | ||
cell.detailLabel.text = item.detail | ||
|
||
return cell | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | ||
return sections[(indexPath as NSIndexPath).section].collapsed ? 0 : UITableViewAutomaticDimension | ||
} | ||
|
||
// Header | ||
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | ||
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header") | ||
|
||
header.titleLabel.text = sections[section].name | ||
header.arrowLabel.text = ">" | ||
header.setCollapsed(sections[section].collapsed) | ||
|
||
header.section = section | ||
header.delegate = self | ||
|
||
return header | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | ||
return 44.0 | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { | ||
return 1.0 | ||
} | ||
|
||
} | ||
|
||
// | ||
// MARK: - Section Header Delegate | ||
// | ||
extension CollapsibleTableSectionViewController: CollapsibleTableViewHeaderDelegate { | ||
|
||
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) { | ||
let collapsed = !sections[section].collapsed | ||
|
||
// Toggle collapse | ||
sections[section].collapsed = collapsed | ||
header.setCollapsed(collapsed) | ||
|
||
tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// CollapsibleTableViewCell.swift | ||
// CollapsibleTableSectionViewController | ||
// | ||
// Created by Yong Su on 7/20/17. | ||
// Copyright © 2017 jeantimex. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class CollapsibleTableViewCell: UITableViewCell { | ||
|
||
let nameLabel = UILabel() | ||
let detailLabel = UILabel() | ||
|
||
// MARK: Initalizers | ||
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | ||
super.init(style: style, reuseIdentifier: reuseIdentifier) | ||
|
||
let marginGuide = contentView.layoutMarginsGuide | ||
|
||
// configure nameLabel | ||
contentView.addSubview(nameLabel) | ||
nameLabel.translatesAutoresizingMaskIntoConstraints = false | ||
nameLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true | ||
nameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true | ||
nameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true | ||
nameLabel.numberOfLines = 0 | ||
nameLabel.font = UIFont.systemFont(ofSize: 16) | ||
|
||
// configure detailLabel | ||
contentView.addSubview(detailLabel) | ||
detailLabel.lineBreakMode = .byWordWrapping | ||
detailLabel.translatesAutoresizingMaskIntoConstraints = false | ||
detailLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true | ||
detailLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true | ||
detailLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true | ||
detailLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 5).isActive = true | ||
detailLabel.numberOfLines = 0 | ||
detailLabel.font = UIFont.systemFont(ofSize: 12) | ||
detailLabel.textColor = UIColor.lightGray | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// CollapsibleTableViewHeader.swift | ||
// CollapsibleTableSectionViewController | ||
// | ||
// Created by Yong Su on 7/20/17. | ||
// Copyright © 2017 jeantimex. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol CollapsibleTableViewHeaderDelegate { | ||
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) | ||
} | ||
|
||
class CollapsibleTableViewHeader: UITableViewHeaderFooterView { | ||
|
||
var delegate: CollapsibleTableViewHeaderDelegate? | ||
var section: Int = 0 | ||
|
||
let titleLabel = UILabel() | ||
let arrowLabel = UILabel() | ||
|
||
override init(reuseIdentifier: String?) { | ||
super.init(reuseIdentifier: reuseIdentifier) | ||
|
||
// | ||
// Constraint the size of arrow label for auto layout | ||
// | ||
arrowLabel.widthAnchor.constraint(equalToConstant: 12).isActive = true | ||
|
||
titleLabel.translatesAutoresizingMaskIntoConstraints = false | ||
arrowLabel.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
contentView.addSubview(titleLabel) | ||
contentView.addSubview(arrowLabel) | ||
|
||
// | ||
// Call tapHeader when tapping on this header | ||
// | ||
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:)))) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
|
||
contentView.backgroundColor = UIColor(hex: 0x2E3944) | ||
|
||
titleLabel.textColor = UIColor.white | ||
arrowLabel.textColor = UIColor.white | ||
|
||
// | ||
// Autolayout the lables | ||
// | ||
let views = [ | ||
"titleLabel" : titleLabel, | ||
"arrowLabel" : arrowLabel, | ||
] | ||
|
||
contentView.addConstraints(NSLayoutConstraint.constraints( | ||
withVisualFormat: "H:|-20-[titleLabel]-[arrowLabel]-20-|", | ||
options: [], | ||
metrics: nil, | ||
views: views | ||
)) | ||
|
||
contentView.addConstraints(NSLayoutConstraint.constraints( | ||
withVisualFormat: "V:|-[titleLabel]-|", | ||
options: [], | ||
metrics: nil, | ||
views: views | ||
)) | ||
|
||
contentView.addConstraints(NSLayoutConstraint.constraints( | ||
withVisualFormat: "V:|-[arrowLabel]-|", | ||
options: [], | ||
metrics: nil, | ||
views: views | ||
)) | ||
} | ||
|
||
// | ||
// Trigger toggle section when tapping on the header | ||
// | ||
func tapHeader(_ gestureRecognizer: UITapGestureRecognizer) { | ||
guard let cell = gestureRecognizer.view as? CollapsibleTableViewHeader else { | ||
return | ||
} | ||
|
||
delegate?.toggleSection(self, section: cell.section) | ||
} | ||
|
||
func setCollapsed(_ collapsed: Bool) { | ||
// | ||
// Animate the arrow rotation (see Extensions.swf) | ||
// | ||
arrowLabel.rotate(collapsed ? 0.0 : .pi / 2) | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Extensions.swift | ||
// CollapsibleTableSectionViewController | ||
// | ||
// Created by Yong Su on 7/20/17. | ||
// Copyright © 2017 jeantimex. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIColor { | ||
|
||
convenience init(hex:Int, alpha:CGFloat = 1.0) { | ||
self.init( | ||
red: CGFloat((hex & 0xFF0000) >> 16) / 255.0, | ||
green: CGFloat((hex & 0x00FF00) >> 8) / 255.0, | ||
blue: CGFloat((hex & 0x0000FF) >> 0) / 255.0, | ||
alpha: alpha | ||
) | ||
} | ||
|
||
} | ||
|
||
extension UIView { | ||
|
||
func rotate(_ toValue: CGFloat, duration: CFTimeInterval = 0.2) { | ||
let animation = CABasicAnimation(keyPath: "transform.rotation") | ||
|
||
animation.toValue = toValue | ||
animation.duration = duration | ||
animation.isRemovedOnCompletion = false | ||
animation.fillMode = kCAFillModeForwards | ||
|
||
self.layer.add(animation, forKey: nil) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters