Skip to content

Commit

Permalink
Merge pull request #5 from jeantimex/fix-source-files-in-pod-spec
Browse files Browse the repository at this point in the history
Fixed the source file in pod spec.
  • Loading branch information
jeantimex authored Jul 21, 2017
2 parents c5ad8d8 + 453c5bf commit e236d41
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 2 deletions.
122 changes: 122 additions & 0 deletions Classes/CollapsibleTableSectionViewController.swift
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)
}

}
48 changes: 48 additions & 0 deletions Classes/CollapsibleTableViewCell.swift
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")
}

}
104 changes: 104 additions & 0 deletions Classes/CollapsibleTableViewHeader.swift
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)
}

}

37 changes: 37 additions & 0 deletions Classes/Extensions.swift
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)
}

}
4 changes: 2 additions & 2 deletions CollapsibleTableSectionViewController.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "CollapsibleTableSectionViewController"
s.version = "0.0.1"
s.version = "0.0.2"
s.summary = "Swift library to support collapsible sections in a table view."

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

s.source_files = "Classes", "Classes/**/*.{h,m}"
s.source_files = "Classes/**/*.swift"
s.exclude_files = "Classes/Exclude"

# s.public_header_files = "Classes/**/*.h"
Expand Down

0 comments on commit e236d41

Please sign in to comment.