Skip to content

Commit 892fc9b

Browse files
authored
Merge pull request #6 from jeantimex/Use-UITableViewHeaderFooterView
Use ui table view header footer view
2 parents 597f0ce + 4afd583 commit 892fc9b

File tree

7 files changed

+205
-164
lines changed

7 files changed

+205
-164
lines changed

ios-swift-collapsible-table-section.xcodeproj/project.pbxproj

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0A12C8C21D9C3AAF00D0BEE3 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A12C8C11D9C3AAF00D0BEE3 /* Extensions.swift */; };
1011
0A908DEF1CFCAA9200470F33 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A908DEE1CFCAA9200470F33 /* AppDelegate.swift */; };
11-
0A908DF41CFCAA9200470F33 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0A908DF21CFCAA9200470F33 /* Main.storyboard */; };
1212
0A908DF61CFCAA9200470F33 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A908DF51CFCAA9200470F33 /* Assets.xcassets */; };
1313
0A908DF91CFCAA9200470F33 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0A908DF71CFCAA9200470F33 /* LaunchScreen.storyboard */; };
1414
0A908E011CFCAC7500470F33 /* CollapsibleTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A908E001CFCAC7500470F33 /* CollapsibleTableViewController.swift */; };
1515
0A908E031CFCB8D600470F33 /* CollapsibleTableViewHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A908E021CFCB8D600470F33 /* CollapsibleTableViewHeader.swift */; };
1616
/* End PBXBuildFile section */
1717

1818
/* Begin PBXFileReference section */
19+
0A12C8C11D9C3AAF00D0BEE3 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
1920
0A908DEB1CFCAA9200470F33 /* ios-swift-collapsible-table-section.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ios-swift-collapsible-table-section.app"; sourceTree = BUILT_PRODUCTS_DIR; };
2021
0A908DEE1CFCAA9200470F33 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
21-
0A908DF31CFCAA9200470F33 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2222
0A908DF51CFCAA9200470F33 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2323
0A908DF81CFCAA9200470F33 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2424
0A908DFA1CFCAA9200470F33 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -57,12 +57,12 @@
5757
isa = PBXGroup;
5858
children = (
5959
0A908DEE1CFCAA9200470F33 /* AppDelegate.swift */,
60-
0A908DF21CFCAA9200470F33 /* Main.storyboard */,
6160
0A908DF51CFCAA9200470F33 /* Assets.xcassets */,
6261
0A908DF71CFCAA9200470F33 /* LaunchScreen.storyboard */,
6362
0A908DFA1CFCAA9200470F33 /* Info.plist */,
6463
0A908E001CFCAC7500470F33 /* CollapsibleTableViewController.swift */,
6564
0A908E021CFCB8D600470F33 /* CollapsibleTableViewHeader.swift */,
65+
0A12C8C11D9C3AAF00D0BEE3 /* Extensions.swift */,
6666
);
6767
path = "ios-swift-collapsible-table-section";
6868
sourceTree = "<group>";
@@ -128,7 +128,6 @@
128128
files = (
129129
0A908DF91CFCAA9200470F33 /* LaunchScreen.storyboard in Resources */,
130130
0A908DF61CFCAA9200470F33 /* Assets.xcassets in Resources */,
131-
0A908DF41CFCAA9200470F33 /* Main.storyboard in Resources */,
132131
);
133132
runOnlyForDeploymentPostprocessing = 0;
134133
};
@@ -142,20 +141,13 @@
142141
0A908E011CFCAC7500470F33 /* CollapsibleTableViewController.swift in Sources */,
143142
0A908E031CFCB8D600470F33 /* CollapsibleTableViewHeader.swift in Sources */,
144143
0A908DEF1CFCAA9200470F33 /* AppDelegate.swift in Sources */,
144+
0A12C8C21D9C3AAF00D0BEE3 /* Extensions.swift in Sources */,
145145
);
146146
runOnlyForDeploymentPostprocessing = 0;
147147
};
148148
/* End PBXSourcesBuildPhase section */
149149

150150
/* Begin PBXVariantGroup section */
151-
0A908DF21CFCAA9200470F33 /* Main.storyboard */ = {
152-
isa = PBXVariantGroup;
153-
children = (
154-
0A908DF31CFCAA9200470F33 /* Base */,
155-
);
156-
name = Main.storyboard;
157-
sourceTree = "<group>";
158-
};
159151
0A908DF71CFCAA9200470F33 /* LaunchScreen.storyboard */ = {
160152
isa = PBXVariantGroup;
161153
children = (

ios-swift-collapsible-table-section/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
1818
// Override point for customization after application launch.
19+
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
20+
self.window?.makeKeyAndVisible()
21+
self.window?.rootViewController = UINavigationController(rootViewController: CollapsibleTableViewController())
1922
return true
2023
}
2124

ios-swift-collapsible-table-section/Base.lproj/Main.storyboard

Lines changed: 0 additions & 97 deletions
This file was deleted.

ios-swift-collapsible-table-section/CollapsibleTableViewController.swift

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,112 @@
88

99
import UIKit
1010

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
2123
}
2224
}
2325

26+
//
27+
// MARK: - View Controller
28+
//
2429
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-
}
4030

4131
var sections = [Section]()
4232

4333
override func viewDidLoad() {
4434
super.viewDidLoad()
4535

36+
self.title = "Apple Products"
37+
4638
// Initialize the sections array
4739
// Here we have three sections: Mac, iPad, iPhone
4840
sections = [
4941
Section(name: "Mac", items: ["MacBook", "MacBook Air", "MacBook Pro", "iMac", "Mac Pro", "Mac mini", "Accessories", "OS X El Capitan"]),
5042
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"]),
5244
]
5345
}
5446

55-
//
56-
// MARK: - UITableViewDelegate
57-
//
47+
}
48+
49+
//
50+
// MARK: - View Controller DataSource and Delegate
51+
//
52+
extension CollapsibleTableViewController {
53+
5854
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
5955
return sections.count
6056
}
6157

6258
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
6460
}
6561

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")
6865

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]
7367

74-
return header.contentView
68+
return cell
7569
}
7670

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")
7978

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)
8182

82-
return cell
83+
header.section = section
84+
header.delegate = self
85+
86+
return header
8387
}
8488

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
91106

92107
// Toggle collapse
93-
sections[section].collapsed = !collapsed
108+
sections[section].collapsed = collapsed
109+
header.setCollapsed(collapsed)
94110

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()
97117
}
98118

99119
}

0 commit comments

Comments
 (0)