This file contains hidden or 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
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Auto resizing the height of the cell | |
tableView.estimatedRowHeight = 44.0 | |
tableView.rowHeight = UITableViewAutomaticDimension | |
... | |
} |
This file contains hidden or 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
extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate { | |
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) { | |
let collapsed = !sections[section].collapsed | |
// Toggle collapse | |
sections[section].collapsed = collapsed | |
header.setCollapsed(collapsed) | |
// Reload the whole section | |
tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic) |
This file contains hidden or 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
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { | |
return sections[(indexPath as NSIndexPath).section].collapsed ? 0 : UITableViewAutomaticDimension | |
} |
This file contains hidden or 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
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell? ?? UITableViewCell(style: .Default, reuseIdentifier: "cell") | |
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row] | |
return cell | |
} |
This file contains hidden or 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
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("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 | |
} |
This file contains hidden or 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
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return sections[section].collapsed ? 0 : sections[section].items.count | |
} |
This file contains hidden or 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
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return sections.count | |
} |
This file contains hidden or 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
override init(reuseIdentifier: String?) { | |
... | |
// arrowLabel must have fixed width and height | |
arrowLabel.widthAnchor.constraintEqualToConstant(12).active = true | |
arrowLabel.heightAnchor.constraintEqualToConstant(12).active = true | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
arrowLabel.translatesAutoresizingMaskIntoConstraints = false | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() |
This file contains hidden or 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
protocol CollapsibleTableViewHeaderDelegate { | |
func toggleSection(header: CollapsibleTableViewHeader, section: Int) | |
} | |
class CollapsibleTableViewHeader: UITableViewHeaderFooterView { | |
var delegate: CollapsibleTableViewHeaderDelegate? | |
var section: Int = 0 | |
... | |
override init(reuseIdentifier: String?) { | |
super.init(reuseIdentifier: reuseIdentifier) | |
... |