|
8 | 8 |
|
9 | 9 | import UIKit |
10 | 10 |
|
11 | | -extension UIView { |
12 | | - func rotate(toValue: CGFloat, duration: CFTimeInterval = 0.2) { |
13 | | - let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") |
14 | | - |
15 | | - rotateAnimation.toValue = toValue |
16 | | - rotateAnimation.duration = duration |
17 | | - rotateAnimation.removedOnCompletion = false |
18 | | - rotateAnimation.fillMode = kCAFillModeForwards |
19 | | - |
20 | | - self.layer.addAnimation(rotateAnimation, forKey: nil) |
| 11 | +// |
| 12 | +// MARK: - Section Data Structure |
| 13 | +// |
| 14 | +struct Section { |
| 15 | + var name: String! |
| 16 | + var items: [String]! |
| 17 | + var collapsed: Bool! |
| 18 | + |
| 19 | + init(name: String, items: [String], collapsed: Bool = false) { |
| 20 | + self.name = name |
| 21 | + self.items = items |
| 22 | + self.collapsed = collapsed |
21 | 23 | } |
22 | 24 | } |
23 | 25 |
|
| 26 | +// |
| 27 | +// MARK: - View Controller |
| 28 | +// |
24 | 29 | class CollapsibleTableViewController: UITableViewController { |
25 | | - |
26 | | - // |
27 | | - // MARK: - Data |
28 | | - // |
29 | | - struct Section { |
30 | | - var name: String! |
31 | | - var items: [String]! |
32 | | - var collapsed: Bool! |
33 | | - |
34 | | - init(name: String, items: [String], collapsed: Bool = false) { |
35 | | - self.name = name |
36 | | - self.items = items |
37 | | - self.collapsed = collapsed |
38 | | - } |
39 | | - } |
40 | 30 |
|
41 | 31 | var sections = [Section]() |
42 | 32 |
|
43 | 33 | override func viewDidLoad() { |
44 | 34 | super.viewDidLoad() |
45 | 35 |
|
| 36 | + self.title = "Apple Products" |
| 37 | + |
46 | 38 | // Initialize the sections array |
47 | 39 | // Here we have three sections: Mac, iPad, iPhone |
48 | 40 | sections = [ |
49 | 41 | Section(name: "Mac", items: ["MacBook", "MacBook Air", "MacBook Pro", "iMac", "Mac Pro", "Mac mini", "Accessories", "OS X El Capitan"]), |
50 | 42 | Section(name: "iPad", items: ["iPad Pro", "iPad Air 2", "iPad mini 4", "Accessories"]), |
51 | | - Section(name: "iPhone", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"]) |
| 43 | + Section(name: "iPhone", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"]), |
52 | 44 | ] |
53 | 45 | } |
54 | 46 |
|
55 | | - // |
56 | | - // MARK: - UITableViewDelegate |
57 | | - // |
| 47 | +} |
| 48 | + |
| 49 | +// |
| 50 | +// MARK: - View Controller DataSource and Delegate |
| 51 | +// |
| 52 | +extension CollapsibleTableViewController { |
| 53 | + |
58 | 54 | override func numberOfSectionsInTableView(tableView: UITableView) -> Int { |
59 | 55 | return sections.count |
60 | 56 | } |
61 | 57 |
|
62 | 58 | override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
63 | | - return (sections[section].collapsed!) ? 0 : sections[section].items.count |
| 59 | + return sections[section].items.count |
64 | 60 | } |
65 | 61 |
|
66 | | - override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { |
67 | | - let header = tableView.dequeueReusableCellWithIdentifier("header") as! CollapsibleTableViewHeader |
| 62 | + // Cell |
| 63 | + override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { |
| 64 | + let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell? ?? UITableViewCell(style: .Default, reuseIdentifier: "cell") |
68 | 65 |
|
69 | | - header.toggleButton.tag = section |
70 | | - header.titleLabel.text = sections[section].name |
71 | | - header.toggleButton.rotate(sections[section].collapsed! ? 0.0 : CGFloat(M_PI_2)) |
72 | | - header.toggleButton.addTarget(self, action: #selector(CollapsibleTableViewController.toggleCollapse), forControlEvents: .TouchUpInside) |
| 66 | + cell.textLabel?.text = sections[indexPath.section].items[indexPath.row] |
73 | 67 |
|
74 | | - return header.contentView |
| 68 | + return cell |
75 | 69 | } |
76 | 70 |
|
77 | | - override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { |
78 | | - let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell! |
| 71 | + override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { |
| 72 | + return sections[indexPath.section].collapsed! ? 0 : 44.0 |
| 73 | + } |
| 74 | + |
| 75 | + // Header |
| 76 | + override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { |
| 77 | + let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header") |
79 | 78 |
|
80 | | - cell.textLabel?.text = sections[indexPath.section].items[indexPath.row] |
| 79 | + header.titleLabel.text = sections[section].name |
| 80 | + header.arrowLabel.text = ">" |
| 81 | + header.setCollapsed(sections[section].collapsed) |
81 | 82 |
|
82 | | - return cell |
| 83 | + header.section = section |
| 84 | + header.delegate = self |
| 85 | + |
| 86 | + return header |
83 | 87 | } |
84 | 88 |
|
85 | | - // |
86 | | - // MARK: - Event Handlers |
87 | | - // |
88 | | - func toggleCollapse(sender: UIButton) { |
89 | | - let section = sender.tag |
90 | | - let collapsed = sections[section].collapsed |
| 89 | + override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
| 90 | + return 44.0 |
| 91 | + } |
| 92 | + |
| 93 | + override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { |
| 94 | + return 1.0 |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +// |
| 100 | +// MARK: - Section Header Delegate |
| 101 | +// |
| 102 | +extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate { |
| 103 | + |
| 104 | + func toggleSection(header: CollapsibleTableViewHeader, section: Int) { |
| 105 | + let collapsed = !sections[section].collapsed |
91 | 106 |
|
92 | 107 | // Toggle collapse |
93 | | - sections[section].collapsed = !collapsed |
| 108 | + sections[section].collapsed = collapsed |
| 109 | + header.setCollapsed(collapsed) |
94 | 110 |
|
95 | | - // Reload section |
96 | | - tableView.reloadSections(NSIndexSet(index: section), withRowAnimation: .Automatic) |
| 111 | + // Adjust the height of the rows inside the section |
| 112 | + tableView.beginUpdates() |
| 113 | + for i in 0 ..< sections[section].items.count { |
| 114 | + tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: i, inSection: section)], withRowAnimation: .Automatic) |
| 115 | + } |
| 116 | + tableView.endUpdates() |
97 | 117 | } |
98 | 118 |
|
99 | 119 | } |
0 commit comments