|
| 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 | +} |
0 commit comments