|
| 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 | + self.title = "Apple Products" |
| 51 | + |
| 52 | + // Initialize the sections array |
| 53 | + // Here we have three sections: Mac, iPad, iPhone |
| 54 | + sections = [ |
| 55 | + Section(name: "Mac", items: [ |
| 56 | + Item(name: "MacBook", detail: "Apple's ultraportable laptop, trading portability for speed and connectivity."), |
| 57 | + Item(name: "MacBook Air", detail: "While the screen could be sharper, the updated 11-inch MacBook Air is a very light ultraportable that offers great performance and battery life for the price."), |
| 58 | + Item(name: "MacBook Pro", detail: "Retina Display The brightest, most colorful Mac notebook display ever. The display in the MacBook Pro is the best ever in a Mac notebook."), |
| 59 | + Item(name: "iMac", detail: "iMac combines enhanced performance with our best ever Retina display for the ultimate desktop experience in two sizes."), |
| 60 | + Item(name: "Mac Pro", detail: "Mac Pro is equipped with pro-level graphics, storage, expansion, processing power, and memory. It's built for creativity on an epic scale."), |
| 61 | + Item(name: "Mac mini", detail: "Mac mini is an affordable powerhouse that packs the entire Mac experience into a 7.7-inch-square frame."), |
| 62 | + Item(name: "OS X El Capitan", detail: "The twelfth major release of OS X (now named macOS)."), |
| 63 | + Item(name: "Accessories", detail: "") |
| 64 | + ]), |
| 65 | + Section(name: "iPad", items: [ |
| 66 | + Item(name: "iPad Pro", detail: "iPad Pro delivers epic power, in 12.9-inch and a new 10.5-inch size."), |
| 67 | + Item(name: "iPad Air 2", detail: "The second-generation iPad Air tablet computer designed, developed, and marketed by Apple Inc."), |
| 68 | + Item(name: "iPad mini 4", detail: "iPad mini 4 puts uncompromising performance and potential in your hand."), |
| 69 | + Item(name: "Accessories", detail: "") |
| 70 | + ]), |
| 71 | + Section(name: "iPhone", items: [ |
| 72 | + Item(name: "iPhone 6s", detail: "The iPhone 6S has a similar design to the 6 but updated hardware, including a strengthened chassis and upgraded system-on-chip, a 12-megapixel camera, improved fingerprint recognition sensor, and LTE Advanced support."), |
| 73 | + Item(name: "iPhone 6", detail: "The iPhone 6 and iPhone 6 Plus are smartphones designed and marketed by Apple Inc."), |
| 74 | + Item(name: "iPhone SE", detail: "The iPhone SE was received positively by critics, who noted its familiar form factor and design, improved hardware over previous 4-inch iPhone models, as well as its overall performance and battery life."), |
| 75 | + Item(name: "Accessories", detail: "") |
| 76 | + ]) |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +// |
| 83 | +// MARK: - View Controller DataSource and Delegate |
| 84 | +// |
| 85 | +extension CollapsibleTableSectionViewController { |
| 86 | + |
| 87 | + override func numberOfSections(in tableView: UITableView) -> Int { |
| 88 | + return sections.count |
| 89 | + } |
| 90 | + |
| 91 | + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 92 | + return sections[section].collapsed ? 0 : sections[section].items.count |
| 93 | + } |
| 94 | + |
| 95 | + // Cell |
| 96 | + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 97 | + let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ?? |
| 98 | + CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell") |
| 99 | + |
| 100 | + let item: Item = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row] |
| 101 | + |
| 102 | + cell.nameLabel.text = item.name |
| 103 | + cell.detailLabel.text = item.detail |
| 104 | + |
| 105 | + return cell |
| 106 | + } |
| 107 | + |
| 108 | + override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { |
| 109 | + return sections[(indexPath as NSIndexPath).section].collapsed ? 0 : UITableViewAutomaticDimension |
| 110 | + } |
| 111 | + |
| 112 | + // Header |
| 113 | + override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { |
| 114 | + let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header") |
| 115 | + |
| 116 | + header.titleLabel.text = sections[section].name |
| 117 | + header.arrowLabel.text = ">" |
| 118 | + header.setCollapsed(sections[section].collapsed) |
| 119 | + |
| 120 | + header.section = section |
| 121 | + header.delegate = self |
| 122 | + |
| 123 | + return header |
| 124 | + } |
| 125 | + |
| 126 | + override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
| 127 | + return 44.0 |
| 128 | + } |
| 129 | + |
| 130 | + override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { |
| 131 | + return 1.0 |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +// |
| 137 | +// MARK: - Section Header Delegate |
| 138 | +// |
| 139 | +extension CollapsibleTableSectionViewController: CollapsibleTableViewHeaderDelegate { |
| 140 | + |
| 141 | + func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) { |
| 142 | + let collapsed = !sections[section].collapsed |
| 143 | + |
| 144 | + // Toggle collapse |
| 145 | + sections[section].collapsed = collapsed |
| 146 | + header.setCollapsed(collapsed) |
| 147 | + |
| 148 | + tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic) |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments